-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
181 lines (142 loc) · 5.08 KB
/
script.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
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir
from typing import Union
class Robot:
def __init__(self, pos: Vec2) -> 'Robot':
self.pos = Vec2(pos)
def process(self, instructions: str, grid: Grid):
for instr in instructions:
if instr == '\n':
continue
self.move(Dir.map_nswe('^v<>')[instr], grid)
if aoc.args.verbose:
print(instr)
print(grid)
def move(self, dir: Dir, grid: Grid) -> bool:
if grid[self.pos + dir].move(dir, grid):
grid[self.pos] = Air(self.pos)
self.pos += dir
grid[self.pos] = self
return True
return False
def __str__(self) -> str:
return '@'
class Box:
def __init__(self, pos: Vec2) -> 'Box':
self.pos = Vec2(pos)
self.width = 1
self.last_draw = -1
def touching(self, dir: Dir, grid: Grid) -> set[Union['Box', 'Wall', 'Air']]:
if dir in {Dir.NORTH, Dir.SOUTH}:
cells = filter(lambda c: type(c) is not Air,
(grid[self.pos + Vec2(Dir.EAST) * i + dir]
for i in range(self.width)))
else:
cells = filter(lambda c: type(c) is not Air,
[grid[self.pos + (dir if dir == Dir.WEST else Vec2(dir) * self.width)]])
cells = set(cells)
return cells
def can_move(self, dir: Dir, grid: Grid) -> bool:
return all(cell.can_move(dir, grid) for cell in self.touching(dir, grid))
def move(self, dir: Dir, grid: Grid) -> bool:
if not self.can_move(dir, grid):
return False
for cell in self.touching(dir, grid):
grid[cell.pos].move(dir, grid)
self.pos += dir
if dir == Dir.WEST:
grid[self.pos + Vec2(Dir.EAST) * self.width] = Air(None)
for i in range(self.width):
grid[self.pos + Vec2(Dir.EAST) * i] = self
elif dir == Dir.EAST:
grid[self.pos + Vec2(Dir.WEST)] = Air(None)
for i in range(self.width):
grid[self.pos + Vec2(Dir.EAST) * i] = self
else:
for i in range(self.width):
grid[self.pos + Vec2(Dir.EAST) * i - dir] = Air(None)
grid[self.pos + Vec2(Dir.EAST) * i] = self
return True
def gps(self) -> int:
return 100 * self.pos.y + self.pos.x
def __str__(self) -> str:
if self.width == 1:
return 'O'
elif self.width == 2:
self.last_draw += 1
if self.last_draw > 1:
self.last_draw = 0
return '[]'[self.last_draw]
def __repr__(self) -> str:
return f"Box at {self.pos}"
class Wall:
def __init__(self, pos: Vec2) -> 'Wall':
self.pos = Vec2(pos)
def move(self, dir: Dir, grid: Grid) -> bool:
return False
def can_move(self, dir: Dir, grid: Grid) -> bool:
return False
def __str__(self) -> str:
return '#'
def __repr__(self) -> str:
return f"Wall at {self.pos}"
class Air:
def __init__(self, pos: Vec2) -> 'Air':
pass
def move(self, dir: Dir, grid: Grid) -> bool:
return True
def can_move(self, dir: Dir, grid: Grid) -> bool:
return True
def __str__(self) -> str:
return '.'
def build_warehouse(floorplan: str, wide: bool = False) -> tuple[Grid, Robot, list[Box]]:
floorplan = floorplan.splitlines()
if wide:
new_floorplan = []
for row in floorplan:
new_row = []
for c in row:
new_row.append({'#': '##', '@': '@.', 'O': '[]', '.': '..'}[c])
new_floorplan.append(''.join(new_row))
floorplan = new_floorplan
warehouse = Grid(floorplan)
robot = None
boxes = []
for cell, pos in warehouse.by_cell():
if cell == ']':
warehouse[pos] = warehouse[Vec2(pos) + Dir.WEST]
warehouse[pos].width = 2
continue
warehouse[pos] = convert(cell, pos)
if type(warehouse[pos]) is Box:
boxes.append(warehouse[pos])
elif type(warehouse[pos]) is Robot:
robot = warehouse[pos]
return warehouse, robot, boxes
def convert(cell: str, pos: Vec2) -> Robot | Box | Wall | Air:
return {
'@': Robot,
'O': Box,
'[': Box,
'#': Wall,
'.': Air
}[cell](pos)
def main():
floorplan, instructions = aoc.read_sections()
warehouse, robot, boxes = build_warehouse(floorplan)
if aoc.args.verbose or aoc.args.progress:
print(warehouse)
robot.process(instructions, warehouse)
if aoc.args.progress:
print(warehouse)
print(f"p1: {sum(box.gps() for box in boxes)}")
warehouse, robot, boxes = build_warehouse(floorplan, wide=True)
if aoc.args.verbose or aoc.args.progress:
print(warehouse)
robot.process(instructions, warehouse)
if aoc.args.progress:
print(warehouse)
print(f"p2: {sum(box.gps() for box in boxes)}")
if __name__ == "__main__":
main()