-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday11.py
93 lines (73 loc) · 2.63 KB
/
day11.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
import numpy as np
import matplotlib.pyplot as plt
from typing import List
from Computer import Computer, InputInterrupt, OutputInterrupt
from collections import defaultdict
def parse_input(filename: str) -> List[int]:
return [int(d) for d in open(filename).read().strip().split(',')]
def part1(code: List[int]) -> int:
grid = defaultdict(int)
position = 0
direction = -1j
read_color = True
computer = Computer(code)
while not computer.done:
try:
computer.run()
except InputInterrupt:
# send in the color at the current spot
computer.inputs.append(grid[position])
except OutputInterrupt:
value = computer.outputs[-1]
if read_color:
# get the color to paint - and paint
grid[position] = value
else:
# read direction - change direction - and move forward 1 step
direction = direction * -1j if value == 0 else direction * 1j
position += direction
read_color = not read_color
# return the visited squares
return len(grid)
def part2(code: List[int]) -> int:
grid = defaultdict(int)
position = 0
grid[0] = 1
direction = -1j
read_color = True
computer = Computer(code)
while not computer.done:
try:
computer.run()
except InputInterrupt:
# send in the color at the current spot
computer.inputs.append(grid[position])
except OutputInterrupt:
value = computer.outputs[-1]
if read_color:
# get the color to paint - and paint
grid[position] = value
else:
# read direction - change direction - and move forward 1 step
direction = direction * -1j if value == 0 else direction * 1j
position += direction
read_color = not read_color
positions = grid.keys()
min_x = int(min(position.real for position in positions))
max_x = int(max(position.real for position in positions))
min_y = int(min(position.imag for position in positions))
max_y = int(max(position.imag for position in positions))
board = np.zeros((max_y - min_y + 1, max_x - min_x + 1))
for pos in grid:
if grid[pos] == 1:
board[int(pos.imag) - min_y][int(pos.real) - min_x] = 1
plt.imshow(board)
plt.show()
return len(grid)
def main():
code = parse_input('input/day11.txt')
print(f'Part 1: {part1(code)}')
code = parse_input('input/day11.txt')
print(f'Part 2: {part2(code)}')
if __name__ == "__main__":
main()