-
Notifications
You must be signed in to change notification settings - Fork 1
/
day17_vacuum_bot.py
145 lines (115 loc) · 3.47 KB
/
day17_vacuum_bot.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
from enum import IntEnum
from utils import IntCodeComputer, Halt
class Tile:
EMPTY = 0
SCAFFOLD = 1
ROBOT_FACING_UP = 2
ROBOT_FACING_DOWN = 3
ROBOT_FACING_LEFT = 4
ROBOT_FACING_RIGHT = 5
ASCII_TO_TILE = {
10: "\n",
35: Tile.SCAFFOLD,
46: Tile.EMPTY,
60: Tile.ROBOT_FACING_LEFT,
62: Tile.ROBOT_FACING_RIGHT,
94: Tile.ROBOT_FACING_UP,
118: Tile.ROBOT_FACING_DOWN,
}
TILE_TO_ASCII = {
Tile.SCAFFOLD: "#",
Tile.EMPTY: ".",
Tile.ROBOT_FACING_DOWN: "v",
Tile.ROBOT_FACING_LEFT: "<",
Tile.ROBOT_FACING_UP: "^",
Tile.ROBOT_FACING_RIGHT: ">",
}
class RobotInterface:
"""Camera and Robot Interface
Aft Scaffolding Control and Information Interface
(ASCII, your puzzle input), provides access to the
cameras and the vacuum robot.
"""
def __init__(self, program):
self.cpu = IntCodeComputer(
program, pause_on_output=False, memory_size=4096, propogate_exceptions=True
)
def run(self):
"""This grabs the screen output"""
try:
self.cpu.process()
except Halt:
return self.cpu.captured_output[:]
raise ValueError("Unrreachable")
def screen_to_grid(screen):
grid = []
line = []
for item in screen:
tile = ASCII_TO_TILE[item]
if tile == "\n":
grid.append(line)
line = []
else:
line.append(tile)
grid.pop() # remove empty row
return grid
def draw_grid(grid):
output = ""
for line in grid:
output += "".join([TILE_TO_ASCII[col] for col in line]) + "\n"
return output[:-1]
def find_intersections(grid):
intersections = []
for y, row in enumerate(grid):
for x, col in enumerate(row):
if grid[y][x] == Tile.SCAFFOLD:
if is_intersection(grid, (x, y)):
intersections.append((x, y))
return intersections
def is_intersection(grid, point_to_check):
x, y = point_to_check
x_min = 0
x_max = len(grid[0])
y_min = 0
y_max = len(grid)
for x_delta, y_delta in [(+1, 0), (-1, 0), (0, -1), (0, +1)]:
new_x = x + x_delta
new_y = y + y_delta
if x_min <= new_x < x_max and y_min <= new_y < y_max:
if grid[new_y][new_x] != grid[y][x]:
return False
else: # point is on the boundary
return False
return True
def calculate_alignment_parameters(intersections):
alignment_parameters = 0
for x, y in intersections:
alignment_parameters += x * y
return alignment_parameters
TEST_INPUT = """..#..........
..#..........
#######...###
#.#...#...#.#
#############
..#...#...#..
..#####...^.."""
def test_calculate_alignment_parameters():
lines = TEST_INPUT.split("\n")
grid = []
for row in lines:
line = []
for item in row:
line.append(ASCII_TO_TILE[ord(item)])
grid.append(line)
intersections = find_intersections(grid)
alignment_parameters = calculate_alignment_parameters(intersections)
assert alignment_parameters == 76
if __name__ == "__main__":
with open("2019/data/day17_input.txt", "r") as f:
intcode_program = f.readline().strip()
robot = RobotInterface(intcode_program)
screen = robot.run()
grid = screen_to_grid(screen)
intersections = find_intersections(grid)
alignment_parameters = calculate_alignment_parameters(intersections)
print(f"Sum of alignment parameters is {alignment_parameters}")