Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aidancbrady/93e45d791e95ec7e3604b6487ccdbf60 to your computer and use it in GitHub Desktop.
Save aidancbrady/93e45d791e95ec7e3604b6487ccdbf60 to your computer and use it in GitHub Desktop.
public static List<int[][]> interpret(String path) {
List<int[][]> ret = new ArrayList<>();
try {
File file = new File(path);
BufferedReader reader = new BufferedReader(new FileReader(file));
String s;
while((s = reader.readLine()) != null) {
s = s.trim();
if (!s.isEmpty()) {
int maxSize = (int)Math.sqrt(s.length());
int[][] grid = new int[maxSize][maxSize];
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int val = c == '.' ? 0 : Integer.parseInt(Character.toString(c));
grid[i/maxSize][i%maxSize] = val;
}
ret.add(grid);
}
}
reader.close();
} catch(Exception e) {
e.printStackTrace();
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment