-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.py
executable file
·86 lines (67 loc) · 1.77 KB
/
day02.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import sys
def run_program(program, patch=None):
ip = 0
mem = dict(enumerate(program))
if patch:
for i, v in patch.items():
mem[i] = v
def op_add(x, y, z):
nonlocal mem, ip
mem[z] = mem[x] + mem[y]
ip += 4
def op_mul(x, y, z):
nonlocal mem, ip
mem[z] = mem[x] * mem[y]
ip += 4
def op_halt(*args):
nonlocal ip
ip = -1
opcodes = {
1: op_add,
2: op_mul,
99: op_halt,
}
while 0 <= ip < len(program):
op = mem[ip]
f = opcodes[op]
f(mem[ip+1], mem[ip+2], mem[ip+3])
return mem[0]
def solve(fn='data/day02.txt'):
with open(fn) as fp:
data = fp.read()
program = list(map(int, data.split(',')))
patch = {1:12, 2:2}
x = run_program(program, patch)
print('Part 1:', x)
goal = 19690720
for a in range(100):
for b in range(100):
patch = {1: a, 2: b}
x = run_program(program, patch)
if x == goal:
print('Part 2:', 100 * a + b, (a, b))
return
# example binary search part 2
(la, lb), (ra, rb) = (0, 0), (99, 99)
while (la, lb) <= (ra, rb):
if la == ra:
a, b = la, (lb + rb) // 2
else:
a, b = (la + ra) // 2, 0
patch = {1: a, 2: b}
x = run_program(program, patch)
if x == goal:
print('Part 2:', 100 * a + b)
if x > goal:
if b > 0:
ra, rb = a, b - 1
else:
ra, rb = a - 1, 99
else:
if b < 99:
la, lb = a, b + 1
else:
la, lb = a + 1, 0
if __name__ == '__main__':
solve(*sys.argv[1:])