-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.py
184 lines (155 loc) · 6.1 KB
/
solution.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
182
183
184
from dataclasses import dataclass
from typing import List, Dict, Set
from enum import Enum
class Cell(str, Enum):
WALL = '#'
EMPTY = '.'
ROBOT = '@'
BOX = 'O'
BOX_LEFT = '['
BOX_RIGHT = ']'
class Direction(str, Enum):
UP = '^'
RIGHT = '>'
DOWN = 'v'
LEFT = '<'
@dataclass(frozen=True)
class Pos:
row: int
col: int
def __add__(self, other):
return Pos(self.row + other.row, self.col + other.col)
DIRECTIONS = {
Direction.UP: Pos(-1, 0),
Direction.RIGHT: Pos(0, 1),
Direction.DOWN: Pos(1, 0),
Direction.LEFT: Pos(0, -1)
}
def parse_input(input_file: str) -> tuple[List[List[str]], str, Pos]:
grid = []
moves = []
robot_pos = None
with open(input_file) as f:
for i, line in enumerate(f.readlines()):
if line.startswith(Cell.WALL):
row = list(line.strip())
if Cell.ROBOT in row:
j = row.index(Cell.ROBOT)
robot_pos = Pos(i, j)
row[j] = Cell.EMPTY
grid.append(row)
else:
moves.append(line.strip())
return grid, ''.join(moves), robot_pos
def scale_map(grid: List[str]) -> List[List[str]]:
scaled = []
for row in grid:
new_row = []
for cell in row:
if cell == Cell.WALL:
new_row.extend([Cell.WALL, Cell.WALL])
elif cell == Cell.BOX:
new_row.extend([Cell.BOX_LEFT, Cell.BOX_RIGHT])
elif cell == Cell.EMPTY:
new_row.extend([Cell.EMPTY, Cell.EMPTY])
elif cell == Cell.ROBOT:
new_row.extend([Cell.ROBOT, Cell.EMPTY])
scaled.append(new_row)
return scaled
def simulate_move(grid: List[List[str]], robot_pos: Pos, move: str) -> tuple[List[List[str]], Pos]:
dest = robot_pos + DIRECTIONS[move]
if grid[dest.row][dest.col] == Cell.EMPTY:
grid[robot_pos.row][robot_pos.col] = Cell.EMPTY
grid[dest.row][dest.col] = Cell.ROBOT
return grid, dest
elif grid[dest.row][dest.col] == Cell.BOX:
temp = dest
while grid[temp.row][temp.col] == Cell.BOX:
temp = temp + DIRECTIONS[move]
if grid[temp.row][temp.col] == Cell.EMPTY:
grid[dest.row][dest.col] = Cell.EMPTY
grid[temp.row][temp.col] = Cell.BOX
grid[robot_pos.row][robot_pos.col] = Cell.EMPTY
grid[dest.row][dest.col] = Cell.ROBOT
return grid, dest
return grid, robot_pos
def calculate_gps_sum(grid: List[List[str]]) -> int:
total = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == Cell.BOX:
total += 100 * i + j
return total
def part1(input_file: str) -> int:
grid, moves, robot_pos = parse_input(input_file)
for move in moves:
grid, robot_pos = simulate_move(grid, robot_pos, move)
return calculate_gps_sum(grid)
def simulate_move_wide(grid: List[List[str]], robot_pos: Pos, move: str) -> tuple[List[List[str]], Pos]:
dest = robot_pos + DIRECTIONS[move]
if grid[dest.row][dest.col] == Cell.EMPTY:
grid[robot_pos.row][robot_pos.col] = Cell.EMPTY
grid[dest.row][dest.col] = Cell.ROBOT
return grid, dest
elif grid[dest.row][dest.col] in [Cell.BOX_LEFT, Cell.BOX_RIGHT]:
q = [dest]
boxes = {dest}
while q:
current = q.pop()
if grid[current.row][current.col] == Cell.BOX_LEFT:
next_pos = Pos(current.row, current.col + 1)
else:
next_pos = Pos(current.row, current.col - 1)
if next_pos not in boxes and grid[next_pos.row][next_pos.col] in [Cell.BOX_LEFT, Cell.BOX_RIGHT]:
q.append(next_pos)
boxes.add(next_pos)
next_pos = Pos(current.row + DIRECTIONS[move].row, current.col + DIRECTIONS[move].col)
if next_pos not in boxes and grid[next_pos.row][next_pos.col] in [Cell.BOX_LEFT, Cell.BOX_RIGHT]:
q.append(next_pos)
boxes.add(next_pos)
spaces = set()
for box in boxes:
space = Pos(box.row + DIRECTIONS[move].row, box.col + DIRECTIONS[move].col)
if space not in boxes:
spaces.add(space)
if grid[space.row][space.col] == Cell.WALL:
return grid, robot_pos
if all(grid[space.row][space.col] == Cell.EMPTY for space in spaces):
def push_distance(box):
if move in (Direction.LEFT, Direction.RIGHT):
return abs(box.col - dest.col)
return abs(box.row - dest.row)
for box in sorted(boxes, key=push_distance, reverse=True):
space = Pos(box.row + DIRECTIONS[move].row, box.col + DIRECTIONS[move].col)
grid[box.row][box.col], grid[space.row][space.col] = \
grid[space.row][space.col], grid[box.row][box.col]
grid[robot_pos.row][robot_pos.col] = Cell.EMPTY
grid[dest.row][dest.col] = Cell.ROBOT
return grid, dest
return grid, robot_pos
def calculate_gps_sum_wide(grid: List[List[str]]) -> int:
total = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == Cell.BOX_LEFT:
total += 100 * i + j
return total
def part2(input_file: str) -> int:
grid, moves, robot_pos = parse_input(input_file)
grid = scale_map(grid)
robot_pos = Pos(robot_pos.row, robot_pos.col * 2)
for move in moves:
grid, robot_pos = simulate_move_wide(grid, robot_pos, move)
return calculate_gps_sum_wide(grid)
def main():
from unit_tests import run_tests
if run_tests():
print("\nAll tests passed! Running actual solution...\n")
result1 = part1('input.txt')
result2 = part2('input.txt')
print(f"Part 1: {result1}")
print(f"Part 2: {result2}")
else:
print("\nTests failed! Please fix the issues before running the actual solution.")
if __name__ == '__main__':
main()