-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc_11b.py
170 lines (139 loc) · 4.08 KB
/
aoc_11b.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
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
''' The complete Intcode computer
N. B. Someone wrote an intcode computer in intcode
https://www.reddit.com/r/adventofcode/comments/e7wml1/2019_intcode_computer_in_intcode/
'''
from os import system
fin = open('input_11.txt')
temp = fin.readline().split(',')
fin.close()
program_template = [int(x) for x in temp]
# memory extension
program_template += [0] * 2000
def pexec(p, pc, in_queue, out_queue, rbase):
def g_o(pc, opnum): # get operand
modes = p[pc] // 100
m = [0, 0, 0, 0]
m[1] = modes % 10
modes = modes // 10
m[2] = modes % 10
modes = modes // 10
m[3] = modes % 10
if (opnum == 3): # target address for write operations
if m[3] == 0:
return p[pc + opnum]
else:
return p[pc + opnum] + rbase
if (p[pc] % 100 == 3): # target address for input write
if m[1] == 0:
return p[pc + opnum]
else:
return p[pc + opnum] + rbase
if m[opnum] == 0: # positional, immediate, relative target value
return p[p[pc + opnum]]
elif m[opnum] == 1:
return p[pc + opnum]
elif m[opnum] == 2:
return p[p[pc + opnum] + rbase]
else:
return None
while True:
# decode instruction
opcode = p[pc] % 100
if opcode == 99: # terminate
return 'END', pc, rbase
elif opcode == 1: # add
p[g_o(pc, 3)] = g_o(pc, 1) + g_o(pc, 2)
pc += 4
elif opcode == 2: # multiply
p[g_o(pc, 3)] = g_o(pc, 1) * g_o(pc, 2)
pc += 4
elif opcode == 3: # input
if in_queue == []:
return 'WAIT', pc, rbase
inp = in_queue.pop(0)
p[g_o(pc, 1)] = inp
pc += 2
elif opcode == 4: # print
out_queue.append(g_o(pc, 1))
pc += 2
elif opcode == 5: # jump-if-true
if g_o(pc, 1) != 0:
pc = g_o(pc, 2)
else:
pc += 3
elif opcode == 6: # jump-if-false
if g_o(pc, 1) == 0:
pc = g_o(pc, 2)
else:
pc += 3
elif opcode == 7: # less than
if g_o(pc, 1) < g_o(pc, 2):
p[g_o(pc, 3)] = 1
else:
p[g_o(pc, 3)] = 0
pc += 4
elif opcode == 8: # less than
if g_o(pc, 1) == g_o(pc, 2):
p[g_o(pc, 3)] = 1
else:
p[g_o(pc, 3)] = 0
pc += 4
elif opcode == 9: # change relative base
rbase += g_o(pc, 1)
pc += 2
else: # unknown opcode
return 'ERROR', pc, rbase
def print_canvas(canvas):
system('cls')
char = {-1: ' ', 0: '.', 1: '#', 8: '8'}
for line in canvas:
for element in line:
print(char[element], end='')
print('|')
print('\n')
max_thrust = 0
# computer initial state
pA = program_template[:]
qAin = []
qAout = []
pcA = 0
stateA = 'WAIT'
rbaseA = 0
# canvas and robot position
width = 100
height = 25
cv = [[-1] * width for i in range(height)] # coords in (y,x) order
r_xpos = width // 2
r_ypos = height // 2
cv[r_ypos][r_xpos] = 1
point_to = 0 # 0 up, 1 left, 2 down, 3 right
dyx = [(-1, 0), (0, -1), (1, 0), (0, 1)]
paintcount = 0
print_canvas(cv)
while True:
if cv[r_ypos][r_xpos] < 1:
qAin.append(0)
else:
qAin.append(1)
if stateA == 'WAIT':
stateA, pcA, rbaseA = pexec(pA, pcA, qAin, qAout, rbaseA)
if qAout:
if cv[r_ypos][r_xpos] == -1:
paintcount += 1
if qAout[0] == 1:
cv[r_ypos][r_xpos] = 1
if qAout[0] == 0:
cv[r_ypos][r_xpos] = 0
if qAout[1] == 0:
point_to = (point_to + 1) % 4
if qAout[1] == 1:
point_to = (point_to - 1) % 4
dy, dx = dyx[point_to]
r_xpos += dx
r_ypos += dy
qAout = []
print_canvas(cv)
if stateA == 'END':
break
print_canvas(cv)
print(paintcount)