-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
191 lines (134 loc) · 5.19 KB
/
util.py
File metadata and controls
191 lines (134 loc) · 5.19 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from typing import List, Tuple
def parse_instruction(instruction: int) -> Tuple[Tuple[int, int, int], int]:
padded_instruction = f"{instruction:05}"
mode_3 = int(padded_instruction[0])
mode_2 = int(padded_instruction[1])
mode_1 = int(padded_instruction[2])
opcode = int(padded_instruction[3:5])
return (mode_1, mode_2, mode_3), opcode
def read(data: List[int],
instruction_pointer: int,
parameter_number,
modes: Tuple[int, int, int]) -> int:
parameter_pointer = instruction_pointer + parameter_number
param = data[parameter_pointer]
mode = modes[parameter_number - 1]
if mode == 0:
return data[param]
if mode == 1:
return param
print(f"unknown mode: [{mode}]")
return -1
def write(data: List[int], instruction_pointer: int, parameter_number: int, value: int) -> None:
parameter_pointer = instruction_pointer + parameter_number
param = data[parameter_pointer]
data[param] = value
# pylint: disable=R0912,R0914,R0915,C0301
# pylint does not like to many branches, local vars and statements. I don't care.
def intcode(data: List[int], inputs=None, instruction_pointer: int = 0) -> Tuple[int, List[int], List[int], int, bool]:
"""
>>> intcode([1,0,0,0,99])
(2, [])
>>> intcode([2,3,0,3,99])
(2, [])
>>> intcode([2,2,2,0,99])
(4, [])
>>> intcode([2,4,4,5,99,0])
(2, [])
>>> intcode([1,1,1,4,99,5,6,0,99])
(30, [])
>>> intcode([3,9,8,9,10,9,4,9,99,-1,8], [8])
(3, [1])
>>> intcode([3,9,8,9,10,9,4,9,99,-1,8], [7])
(3, [0])
# less than 8, position
>>> intcode([3,9,7,9,10,9,4,9,99,-1,8], [7])
(3, [1])
>>> intcode([3,9,7,9,10,9,4,9,99,-1,8], [9])
(3, [0])
# equal to 8, immediate
>>> intcode([3,3,1108,-1,8,3,4,3,99], [8])
(3, [1])
>>> intcode([3,3,1108,-1,8,3,4,3,99], [7])
(3, [0])
# less than 8, immediate
>>> intcode([3,3,1107,-1,8,3,4,3,99], [7])
(3, [1])
>>> intcode([3,3,1107,-1,8,3,4,3,99], [9])
(3, [0])
# jump, position
>>> intcode([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9], [1])
(3, [1])
>>> intcode([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9], [0])
(3, [0])
# jump, immediate
>>> intcode([3,3,1105,-1,9,1101,0,0,12,4,12,99,1], [1])
(3, [1])
>>> intcode([3,3,1105,-1,9,1101,0,0,12,4,12,99,1], [0])
(3, [0])
"""
if inputs is None:
inputs = []
outputs: List[int] = []
while True:
modes, opcode = parse_instruction(data[instruction_pointer])
if opcode == 1:
value_1 = read(data, instruction_pointer, 1, modes)
value_2 = read(data, instruction_pointer, 2, modes)
write(data, instruction_pointer, 3, value_1 + value_2)
instruction_pointer_shift = 4
elif opcode == 2:
value_1 = read(data, instruction_pointer, 1, modes)
value_2 = read(data, instruction_pointer, 2, modes)
write(data, instruction_pointer, 3, value_1 * value_2)
instruction_pointer_shift = 4
elif opcode == 3:
if len(inputs) == 0:
return -1, outputs, data, instruction_pointer, False
value_1 = inputs[0]
inputs = inputs[1:]
write(data, instruction_pointer, 1, value_1)
instruction_pointer_shift = 2
elif opcode == 4:
value_1 = read(data, instruction_pointer, 1, modes)
outputs.append(value_1)
instruction_pointer_shift = 2
elif opcode == 5:
condition = read(data, instruction_pointer, 1, modes)
if condition != 0:
new_instruction_pointer = read(data, instruction_pointer, 2, modes)
instruction_pointer = new_instruction_pointer
instruction_pointer_shift = 0
else:
instruction_pointer_shift = 3
elif opcode == 6:
condition = read(data, instruction_pointer, 1, modes)
if condition == 0:
new_instruction_pointer = read(data, instruction_pointer, 2, modes)
instruction_pointer = new_instruction_pointer
instruction_pointer_shift = 0
else:
instruction_pointer_shift = 3
elif opcode == 7:
value_1 = read(data, instruction_pointer, 1, modes)
value_2 = read(data, instruction_pointer, 2, modes)
if value_1 < value_2:
target_value = 1
else:
target_value = 0
write(data, instruction_pointer, 3, target_value)
instruction_pointer_shift = 4
elif opcode == 8:
value_1 = read(data, instruction_pointer, 1, modes)
value_2 = read(data, instruction_pointer, 2, modes)
if value_1 == value_2:
target_value = 1
else:
target_value = 0
write(data, instruction_pointer, 3, target_value)
instruction_pointer_shift = 4
elif opcode == 99:
return data[0], outputs, data, instruction_pointer, True
else:
raise Exception(f"unknown opcode [{opcode}]")
instruction_pointer += instruction_pointer_shift