-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09comp.py
138 lines (107 loc) · 3.01 KB
/
day09comp.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 utils import permutations_of, float_range, deep_loop
import sys
from collections import defaultdict
DEBUG = False
if "--debug" in sys.argv or "-d" in sys.argv:
print("Entering debug mode")
DEBUG = True
class InfiniteTape:
def __init__(self, items):
self.data = defaultdict(int)
for index, i in enumerate(items):
self.data[index] = i
def __getitem__(self, key):
if isinstance(key, slice):
return [self.data[i] for i in range(key.start, key.stop)]
else:
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
data = InfiniteTape(int(i) for i in input("Enter the intcode program\n>>> ").split(","))
class IP:
def __init__(self):
self.address = 0
def set(self, new_address):
self.address = new_address
def get(self):
return self.address
def add(i, a, b, c, ip):
i[c] = a + b
def mul(i, a, b, c, ip):
i[c] = a * b
def take_input(i, a, ip):
i[a] = int(input("Program is requesting input\n>>> "))
def output(i, a, ip):
print(a)
def jnz(i, a, b, ip):
if a:
ip.set(b - 3)
def jz(i, a, b, ip):
if not a:
ip.set(b - 3)
def tlt(i, a, b, c, ip):
if a < b:
i[c] = 1
else:
i[c] = 0
def teq(i, a, b, c, ip):
if a == b:
i[c] = 1
else:
i[c] = 0
def mod_base(i, a, ip):
global relative_base
relative_base += a
relative_base = 0
codes = [
lambda i, ip: None,
add,
mul,
take_input,
output,
jnz,
jz,
tlt,
teq,
mod_base,
]
offsets = [1, 4, 4, 2, 2, 3, 3, 4, 4, 2]
position = IP()
opcode = data[position.get()]
while True:
# if DEBUG:
# print(data)
# input()
third_param_mode, opcode = divmod(opcode, 10000)
second_param_mode, opcode = divmod(opcode, 1000)
first_param_mode, opcode = divmod(opcode, 100)
# print(opcode)
parameters = data[position.get() + 1 : position.get() + offsets[opcode]]
# print(opcode, parameters)
# if len(parameters) == 0:
# print(opcode)
if first_param_mode == 0:
if opcode != 3:
parameters[0] = data[parameters[0]]
if first_param_mode == 2:
# print(opcode, *parameters)
# print(relative_base)
parameters[0] = parameters[0] + relative_base
if opcode != 3:
parameters[0] = data[parameters[0]]
if len(parameters) > 1:
if second_param_mode == 0:
parameters[1] = data[parameters[1]]
if second_param_mode == 2:
parameters[1] = data[parameters[1] + relative_base]
if len(parameters) > 2:
# if third_param_mode == 0:
# parameters[2] = data[parameters[2]]
if third_param_mode == 2:
parameters[2] = parameters[2] + relative_base
# print(codes[opcode].__name__, parameters)
codes[opcode](data, *parameters, position)
position.set(position.get() + offsets[opcode])
opcode = data[position.get()]
if opcode == 99:
break