-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.py
138 lines (125 loc) · 5.1 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from PuzzleInput import ReadInput
from collections import defaultdict
pz_input = ReadInput(9).data[0].split(",")
class Intcode:
logic = []
def __init__(self, logic):
self.logic = defaultdict(lambda: 0)
for i in range(len(logic)):
self.logic[i] = int(logic[i])
self.working_copy = self.logic.copy()
self.i = 0
self.relative_base = 0
def reset(self):
self.working_copy = self.logic.copy()
self.i = 0
def get_value(self, logic, mode, i):
if mode == 0:
return logic[logic[i]]
elif mode == 1:
return logic[i]
elif mode == 2:
return logic[self.relative_base + logic[i]]
def run(self, input):
a_input = self.working_copy
while True:
command = a_input[self.i]
if command == 99:
return None
# Addition
if command % 10 == 1:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
mode_3 = 2 if (command // 10000) % 10 == 2 else 0
val_1 = self.get_value(a_input, mode_1, self.i + 1)
val_2 = self.get_value(a_input, mode_2, self.i + 2)
loc = self.relative_base + a_input[self.i + 3] if mode_3 == 2 else a_input[self.i + 3]
a_input[loc] = val_1 + val_2
self.i += 4
# Multiplication
elif command % 10 == 2:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
mode_3 = 2 if (command // 10000) % 10 == 2 else 0
val_1 = self.get_value(a_input, mode_1, self.i + 1)
val_2 = self.get_value(a_input, mode_2, self.i + 2)
loc = self.relative_base + a_input[self.i + 3] if mode_3 == 2 else a_input[self.i + 3]
a_input[loc] = val_1 * val_2
self.i += 4
# Input
elif command % 10 == 3:
mode = 2 if (command // 100) % 10 == 2 else 0
if mode == 0:
a_input[a_input[self.i + 1]] = input
elif mode == 2:
a_input[self.relative_base + a_input[self.i + 1]] = input
self.i += 2
# Output
elif command % 10 == 4:
mode = (command // 100) % 10
to_output = self.get_value(a_input, mode, self.i + 1)
self.i += 2
return to_output
# Jump if true (non-zero)
elif command % 10 == 5:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
if self.get_value(a_input, mode_1, self.i + 1) != 0:
self.i = self.get_value(a_input, mode_2, self.i + 2)
continue
self.i += 3
# Jump if false (zero)
elif command % 10 == 6:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
if self.get_value(a_input, mode_1, self.i + 1) == 0:
self.i = self.get_value(a_input, mode_2, self.i + 2)
continue
self.i += 3
# Less than
elif command % 10 == 7:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
mode_3 = 2 if (command // 10000) % 10 == 2 else 0
val_1 = self.get_value(a_input, mode_1, self.i + 1)
val_2 = self.get_value(a_input, mode_2, self.i + 2)
loc = self.relative_base + a_input[self.i + 3] if mode_3 == 2 else a_input[self.i + 3]
a_input[loc] = 1 if val_1 < val_2 else 0
self.i += 4
# Equals
elif command % 10 == 8:
mode_1 = (command // 100) % 10
mode_2 = (command // 1000) % 10
mode_3 = 2 if (command // 10000) % 10 == 2 else 0
val_1 = self.get_value(a_input, mode_1, self.i + 1)
val_2 = self.get_value(a_input, mode_2, self.i + 2)
loc = self.relative_base + a_input[self.i + 3] if mode_3 == 2 else a_input[self.i + 3]
a_input[loc] = 1 if val_1 == val_2 else 0
self.i += 4
# Relative base adjust
elif command % 10 == 9:
mode = (command // 100) % 10
self.relative_base += self.get_value(a_input, mode, self.i + 1)
self.i += 2
else:
raise AssertionError("Bad command %s" % command)
def part_a():
# pz_input = [1102,34915192,34915192,7,4,7,99,0]
comp = Intcode(pz_input)
output = comp.run(1)
last_output = output
while output is not None:
last_output = output
output = comp.run(1)
return last_output
def part_b():
comp = Intcode(pz_input)
output = comp.run(2)
last_output = output
while output is not None:
last_output = output
output = comp.run(2)
return last_output
if __name__ == "__main__":
print("Part A", part_a())
print("Part B", part_b())