-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17_1.groovy
68 lines (63 loc) · 1.87 KB
/
day17_1.groovy
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def sampleInput = '''Register A: 729
Register B: 0
Register C: 0
Program: 0,1,5,4,3,0'''
static def parse(List<String> input) {
[
(input[0] - 'Register A: ') as int,
(input[1] - 'Register B: ') as int,
(input[2] - 'Register C: ') as int,
(input[4] - 'Program: ').split(',').collect { it as int } as int[]
]
}
static String emulate(long A, long B, long C, int[] P) {
int IP = 0
def out = new StringBuilder()
while (IP >= 0 && IP < P.length) {
int I = P[IP]
int op = P[IP+1]
long opVal = (op < 4 ? op : (op == 4 ? A : (op == 5 ? B : C)))
println "$IP $I, $op, $opVal, $A, $B, $C"
switch (I) {
case 0: // adv
A >>= opVal
break
case 1: // bxl
B ^= op // Note: literal operand
break
case 2: // bst
B = opVal & 0b111
break
case 3: // jnz
if (A != 0) {
IP = op
}
break
case 4: // bxc
B ^= C
break
case 5: // out
out.append(opVal & 0b111)
println out
break
case 6: // bdv
B = (A >> opVal)
break
case 7: // cdv
C = (A >> opVal)
break
default:
throw new RuntimeException("Invalid instruction at $IP: ${P[IP]}")
}
if (I != 3 || A == 0) { // JNZ
IP += 2
}
}
(0..<out.size()).collect { out[it] }.join(',')
}
static String solve(List<String> input) {
def (A, B, C, P) = parse(input)
emulate(A, B, C, P)
}
assert '4,6,3,5,6,3,5,2,1,0' == solve(sampleInput.split('\n') as List)
println solve(new File('input/day17.txt').readLines())