-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.py
77 lines (57 loc) · 1.77 KB
/
day17.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
from intcode import Computer
from io import StringIO
from collections import defaultdict
from utils import neighbours
class AComputer(Computer):
def __init__(self, program, out):
self.out = out
super().__init__(program)
def process_output(self, o):
self.out.write(chr(o))
def get_input(self):
raise NotImplementedError()
def a(program):
out_buffer = StringIO()
computer = AComputer(program, out_buffer)
computer.run()
s = out_buffer.getvalue()
grid = [list(line) for line in s.splitlines()]
d = {}
for row in range(len(grid)):
for col in range(len(grid[row])):
d[row, col] = grid[row][col]
intersections = []
for pos, v in d.items():
if v == "#":
if all(d.get(n, ".") == "#" for n in neighbours(pos)):
intersections.append(pos)
return sum(x * y for x, y in intersections)
def to_ascii(s):
return list(map(ord, s))
class BComputer(Computer):
def __init__(self, program):
inputs = [
*to_ascii("A,B,A,B,A,C,B,C,A,C\n"), # main
*to_ascii("R,4,L,10,L,10\n"), # A
*to_ascii("L,8,R,12,R,10,R,4\n"), # B
*to_ascii("L,8,L,8,R,10,R,4\n"), # C
*to_ascii("n\n"),
]
self.inputs = iter(inputs)
self.last_output = 0
super().__init__(program)
def process_output(self, o):
print(chr(o), end="")
self.last_output = o
def get_input(self):
return next(self.inputs)
def b(program):
program[0] = 2
computer = BComputer(program)
computer.run()
return computer.last_output
def main():
program = list(map(int, open("input17.txt").read().split(",")))
print(b(program))
if __name__ == "__main__":
main()