Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2014 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/10570453 to your computer and use it in GitHub Desktop.
Save anonymous/10570453 to your computer and use it in GitHub Desktop.
MagicTrick.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class MagicTrick {
private static final String BAD_MAGICIAN = "Bad magician!";
private static final String VOLUNTEER_CHEATED = "Volunteer cheated!";
public static void main(String[] args) {
// Read file
File file = new File(
"/Work/EclipseWorkspace/GoogleCodeJam/src/testcases/A-small-attempt3.in");
BufferedReader reader = null;
String line = "";
ArrayList<String> lines = new ArrayList<>();
try {
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
} catch (IOException e) {
}
// Sets the Case number that will be placed in each iteration of the next loop
int caseNum = 1;
// Iterates over each set of two grids
for (int i = 1; i < lines.size(); i += 10) {
int firstRowNumber = Integer.parseInt(lines.get(i));
int secondRowNumber = Integer.parseInt(lines.get(i+5));
String[] rowOne = lines.get(i + firstRowNumber).split(" ");
String[] rowTwo = lines.get(i + 5 + secondRowNumber).split(" ");
// Get matches
int matches = 0;
int[] temp = new int[4];
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
if (Integer.parseInt(rowOne[j]) == Integer.parseInt(rowTwo[k])) {
matches++;
temp[0] = Integer.parseInt(rowOne[j]);
}
}
}
if (matches == 1) {
System.out.println("Case #" + caseNum++ + ": " + temp[0]);
} else if (matches > 1) {
System.out.println("Case #" + caseNum++ + ": " + BAD_MAGICIAN );
}else {
System.out.println("Case #" + caseNum++ + ": " + VOLUNTEER_CHEATED);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment