Skip to content

Instantly share code, notes, and snippets.

@roccodev
Created December 23, 2017 17:04
Show Gist options
  • Select an option

  • Save roccodev/43bc4f65091c940a74243794df7fac3e to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/43bc4f65091c940a74243794df7fac3e to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day23
public static HashMap<String, Integer> registers = new HashMap<String, Integer>();
public static void main(String args[]) throws IOException {
ArrayList<String> bloccs = new ArrayList<String>(
Files.readAllLines(Paths.get(new File("res/day23").getPath())).stream().collect(Collectors.toList()));
char[] array = "abcdefgh".toCharArray();
for (char c : array)
registers.put(c + "", 0);
int i = 0;
int counter = 0;
while (true) {
boolean hasJumped = false;
String[] data = bloccs.get(i).split(" ");
String mode = data[0];
String f1 = data[1];
String f2 = data[2];
switch (mode) {
case "set":
registers.put(f1, value(f2));
break;
case "mul":
registers.put(f1, registers.get(f1) * value(f2));
counter++;
break;
case "sub":
registers.put(f1, registers.get(f1) - value(f2));
break;
case "jnz":
if(value(f1) != 0) {
i += value(f2);
hasJumped = true;
}
break;
}
if(!hasJumped) i++;
if(i >= bloccs.size()) break;
}
System.out.println(counter);
}
private static boolean isNumber(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private static int value(String s) {
if(isNumber(s)) return Integer.parseInt(s);
return registers.get(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment