-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.py
71 lines (55 loc) · 1.63 KB
/
day19.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
from intcode import Computer
from utils import neighbours
from collections import deque
import itertools
class BeamComputer(Computer):
def __init__(self, program, x, y):
self.inputs = iter([x, y])
self.res = float("nan")
super().__init__(program)
def process_output(self, o):
self.res = o
def get_input(self):
return next(self.inputs)
def probe_location(program, x, y):
computer = BeamComputer(program, x, y)
computer.run()
assert computer.res in [0, 1]
return computer.res
def a(program):
N = 50
res = 0
w = [["" for _ in range(N)] for _ in range(N)]
for x in range(N):
for y in range(N):
s = probe_location(program, x, y)
res += s
w[y][x] = ".#"[s]
print("\n".join("".join(row) for row in w))
return res
def check_square(program, right, top):
S = 100 - 1
return all(
[
probe_location(program, right - S, top),
probe_location(program, right, top + S),
probe_location(program, right - S, top + S),
]
)
def b(program):
pos = (6, 8)
assert probe_location(program, *pos)
while True:
if check_square(program, *pos):
return (pos[0] - 99) * 10_000 + pos[1]
for neighbour in [(pos[0] + 1, pos[1] + 1), (pos[0], pos[1] + 1)]:
if probe_location(program, *neighbour) == 1:
pos = neighbour
break
else:
raise ValueError()
def main():
program = list(map(int, open("input19.txt").read().split(",")))
print(b(program))
if __name__ == "__main__":
main()