-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18_1.py
63 lines (48 loc) · 1.75 KB
/
day18_1.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
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import time
start_time = time.time()
day = 18
is_test = False
input_file = f'./input/day{day}{"_test" if is_test else ""}.txt'
dirs = ((-1, 0), (1, 0), (0, -1), (0, 1))
def build_grid(ls):
return defaultdict(str) | {(i, j): ls[i][j] for i in range(len(ls)) for j in range(len(ls[i]))}
m = 7 if is_test else 71
n = 7 if is_test else 71
total_count = 12 if is_test else 1024
g = build_grid([['.' for i in range(n)] for j in range(m)])
def sol():
def dijkstra(sr, sc, er, ec):
open_list = [(0, sr, sc)]
heapify(open_list)
best_scores = defaultdict(int)
best_scores[(sr, sc)] = 0
closed_set = set()
while open_list:
score, r, c = heappop(open_list)
closed_set.add((r, c))
if best_scores[(r, c)] < score:
continue
if (r, c) == (er, ec):
return score
for idx, (dr, dc) in enumerate(dirs):
nr, nc = r + dr, c + dc
if g[nr, nc] and g[nr, nc] != '#' and (nr, nc) not in closed_set:
new_score = score + 1
if (nr, nc) not in best_scores or new_score < best_scores[nr, nc]:
best_scores[nr, nc] = new_score
heappush(open_list, (new_score, nr, nc))
return None
with open(input_file, 'r') as f:
count = 0
for pos in f.read().splitlines():
if count >= total_count:
break
x, y = pos.split(',')
x, y = int(y), int(x)
g[x, y] = '#'
count += 1
print(dijkstra(0, 0, m - 1, n - 1))
sol()
print(f'{(time.time() - start_time) * 1000:.3f}ms')