-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16.py
More file actions
63 lines (52 loc) · 1.92 KB
/
day16.py
File metadata and controls
63 lines (52 loc) · 1.92 KB
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
# -*- coding: utf-8 -*-
import os
from time import perf_counter
import heapq
def profiler(method):
def profiler_method(*arg, **kw):
t = perf_counter()
ret = method(*arg, **kw)
print(f'{method.__name__} method took : {perf_counter()-t:.4f} sec')
return ret
return profiler_method
def find_all_best_paths(maze):
start = end = None
for i, row in enumerate(maze):
if 'S' in row:
start = (i, row.index('S'))
if 'E' in row:
end = (i, row.index('E'))
priority_queue = [(0, start[0], start[1], (0, 1), [])]
min_score = float('inf')
best_path_tiles = set()
visited = {}
while priority_queue:
score, x, y, (dx, dy), path = heapq.heappop(priority_queue)
current_path = path + [(x, y)]
if (x, y) == end:
if score < min_score:
min_score = score
best_path_tiles = set(current_path)
elif score == min_score:
best_path_tiles.update(current_path)
continue
state_key = (x, y, (dx, dy))
if state_key in visited and visited[state_key] < score:
continue
visited[state_key] = score
for (dx, dy), penalty in [((dx, dy), 1), ((-dy, dx), 1001), ((dy, -dx), 1001)]:
if 0 <= x + dx < len(maze[0]) and 0 <= y + dy < len(maze) and maze[x + dx][y + dy] != '#':
heapq.heappush(priority_queue, (score + penalty, x + dx, y + dy, (dx, dy), current_path))
return min_score, len(best_path_tiles)
def get_input():
with open(os.path.dirname(os.path.realpath(__file__))+'/input', 'r', encoding='utf-8') as f:
content = [s.strip() for s in f.read().rstrip().split('\n')]
return content
@profiler
def solve():
content = get_input()
min_score, tiles = find_all_best_paths(content)
print(f'Part 1: {min_score}')
print(f'Part 2: {tiles}')
if __name__ == "__main__":
solve()