-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc_19b.py
187 lines (150 loc) · 4.56 KB
/
aoc_19b.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
''' 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
import time
fin = open('input_19.txt')
temp = fin.readline().split(',')
fin.close()
program_template = [int(x) for x in temp]
# memory extension
program_template += [0] * 10000
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
# print(pc)
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, ry, rx):
system('cls')
char = {46: ' ', 35: '#', 94: '^', 60: '<',
62: '>', 118: 'v', 0: '.', 1: '#'}
for y, line in enumerate(canvas):
for x, element in enumerate(line):
if x == rx and y == ry:
print('D', end='')
else:
print(char[element], end='')
print('|', y)
print('\n y:', ry, 'x:', rx)
def calib(cv):
width = len(cv[1])
height = len(cv)
checksum = 0
for y in range(1, height - 1):
for x in range(1, width - 1):
if cv[y][x] + cv[y - 1][x] + cv[y + 1][x] + cv[y][x - 1] + cv[y][x + 1] == 5 * 35:
checksum += x * y
return checksum
def run_once(cur_input):
# computer initial state
pA = program_template[:]
qAin = cur_input
qAout = []
pcA = 0
stateA = 'WAIT'
rbaseA = 0
# print(qAin)
while True:
if stateA == 'WAIT':
stateA, pcA, rbaseA = pexec(pA, pcA, qAin, qAout, rbaseA)
response = qAout.pop()
if stateA == 'END':
break
return response
cv = [[-1 for x in range(50)] for y in range(50)]
xbeam = 0
shipfound = False
shipsize = 100
starttime = time.time()
for y in range(8, 4000):
beamfound = False
if shipfound:
break
for x in range(xbeam, 4000):
ul = run_once([x, y])
if ul == 1:
if not beamfound:
xbeam = x
beamfound = True
ur = run_once([x + shipsize - 1, y])
if ur == 0:
break
ll = run_once([x, y + shipsize - 1])
if ll == 1:
print(x, y)
shipfound = True
if ur == 0:
break
print(time.time() - starttime)