-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
103 lines (70 loc) · 2.4 KB
/
day9.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from collections import defaultdict
sample = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"
# with open('day9_input.txt') as f:
# codes = [int(num) for num in f.read().split(',')]
codes = defaultdict(int)
for i, n in enumerate(sample.split(',')):
codes[i] = int(n)
def parse(instructions):
opcode = instructions % 100
mode_p1 = instructions //100 % 10
mode_p2 = instructions //1000 % 10
mode_p3 = instructions //10000 % 10
return opcode, mode_p1, mode_p2, mode_p3
def part_one():
rel_base = 0
i = 0
def get_value(mode, param):
# mode, param = zipped
if mode == 1:
return param
if mode == 2:
return codes[param+rel_base]
if mode == 0:
return codes[param]
opcode = None
while opcode !=99:
opcode, mode_p1, mode_p2, mode_write_to = parse(codes[i])
print(i, opcode, mode_p1, mode_p2, mode_write_to)
write_to = i+3 if mode_write_to == 0 else i+3+rel_base
if opcode == 1:
codes[write_to] = get_value(mode_p1, codes[i+1]) + get_value(mode_p2, codes[i+2])
i += 4
elif opcode == 2:
codes[write_to] = get_value(mode_p1, codes[i+1]) \
* get_value(mode_p2, codes[i+2])
i += 4
elif opcode == 3:
codes[codes[i+1]] = 5 ## Input
i += 2
elif opcode == 4:
print(get_value(mode_p1, codes[i+1]))
i += 2
elif opcode == 5:
if get_value(mode_p1, codes[i+1]):
i = get_value(mode_p2, codes[i+2])
else:
i += 3
elif opcode == 6:
if not get_value(mode_p1, codes[i+1]):
i = get_value(mode_p2, codes[i+2])
else:
i += 3
elif opcode == 7:
if get_value(mode_p1, codes[i+1]) < get_value(mode_p2, codes[i+2]):
codes[write_to] = 1
else:
codes[write_to] = 0
i += 4
elif opcode == 8:
if get_value(mode_p1, codes[i+1]) == get_value(mode_p2, codes[i+2]):
codes[write_to] = 1
else:
codes[write_to] = 0
i += 4
elif opcode == 9:
rel_base += get_value(mode_p1, codes[i+1])
i += 2
elif opcode == 99:
break
part_one()