-
Notifications
You must be signed in to change notification settings - Fork 4
/
05.py
58 lines (44 loc) · 4.08 KB
/
05.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
#!/usr/bin/env python
import intcode
from operator import setitem
with open(__file__, "r") as f:
c = f.read()
initial = list(map(int, c[c.rindex("🎅") + 1 : c.rindex("🐍")].rstrip().split(",")))
def run(inp):
mem = list(map(int, line))
pc = 0
d = {
1: {"p": 4, "op": lambda a, b, c: setitem(mem, c, a + b)},
2: {"p": 4, "op": lambda a, b, c: setitem(mem, c, a * b)},
3: {"p": 2, "op": lambda a, b, c: setitem(mem, a, inp)},
4: {"p": 2, "op": lambda a, b, c: print(mem[a])},
5: {"p": 3, "op": lambda a, b, c: b if a != 0 else pc + 3},
6: {"p": 3, "op": lambda a, b, c: b if a == 0 else pc + 3},
7: {"p": 4, "op": lambda a, b, c: setitem(mem, c, 1 if a < b else 0)},
8: {"p": 4, "op": lambda a, b, c: setitem(mem, c, 1 if a == b else 0)},
}
while mem[pc] != 99:
opcode, *args = mem[pc:][:4]
opcode = str(opcode).zfill(5)
operation = int(opcode[-2:])
if operation in {1, 2, 5, 6, 7, 8}:
if opcode[2] == "0": args[0] = mem[args[0]]
if opcode[1] == "0": args[1] = mem[args[1]]
if 1 <= operation <= 8:
o = d[operation]
width = o["p"]
ret = o["op"](*args)
if operation in {5, 6}:
pc = ret
else:
pc += width
else:
raise Exception(f"run: unknown opcode {opcode}")
part_one = intcode.run(initial, 1)["output"]
print(part_one)
assert part_one == 7286649
part_two = intcode.run(initial, 5)["output"]
print(part_two)
assert part_two == 15724522
"""🎅3,225,1,225,6,6,1100,1,238,225,104,0,1101,11,91,225,1002,121,77,224,101,-6314,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1102,74,62,225,1102,82,7,224,1001,224,-574,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1101,28,67,225,1102,42,15,225,2,196,96,224,101,-4446,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,86,57,225,1,148,69,224,1001,224,-77,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1101,82,83,225,101,87,14,224,1001,224,-178,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1101,38,35,225,102,31,65,224,1001,224,-868,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,57,27,224,1001,224,-84,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,61,78,225,1001,40,27,224,101,-89,224,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,226,224,1002,223,2,223,1006,224,329,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,344,101,1,223,223,1107,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,374,101,1,223,223,7,677,677,224,102,2,223,223,1005,224,389,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,404,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,419,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,434,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,449,1001,223,1,223,7,226,677,224,102,2,223,223,1005,224,464,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,1007,226,677,224,1002,223,2,223,1006,224,494,101,1,223,223,8,677,226,224,1002,223,2,223,1005,224,509,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,524,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,539,101,1,223,223,107,226,677,224,102,2,223,223,1005,224,554,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,569,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,584,101,1,223,223,1107,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,614,101,1,223,223,8,226,226,224,102,2,223,223,1006,224,629,101,1,223,223,108,226,677,224,102,2,223,223,1005,224,644,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,659,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
🐍"""