Created
December 23, 2017 17:04
-
-
Save roccodev/43bc4f65091c940a74243794df7fac3e to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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