-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.java
43 lines (37 loc) · 1.1 KB
/
Day12.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.List;
public class Day12 {
static int part1(int[] registers, List<String> instructions) {
int i = 0;
while (i < instructions.size()) {
String[] instruction = instructions.get(i).split(" ");
String command = instruction[0];
int x = instruction[1].charAt(0);
if (command.equals("cpy")) {
if (x >= '0' && x <= '9') {
x = Integer.parseInt(instruction[1]);
}
else {
x = registers[x - 'a'];
}
int r = instruction[2].charAt(0);
registers[r - 'a'] = x;
}
else if (command.equals("inc")) {
registers[x - 'a']++;
}
else if (command.equals("dec")) {
registers[x - 'a']--;
}
else if (x >= '0' && x <= '9' || x >= 'a' && x <= 'd' && registers[x - 'a'] != 0) { // jnz
int jump = Integer.parseInt(instruction[2]) - 1;
i += jump;
}
i++;
}
return registers[0];
}
public static void main(String[] args) {
List<String> input = Util.readInput("day12.input");
System.out.println(part1(new int[] { 0, 0, 1, 0 }, input));
}
}