-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21_1.py
124 lines (100 loc) · 3.72 KB
/
day21_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
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
from collections import defaultdict
import time
from functools import cache
from heapq import heapify, heappop, heappush
start_time = time.time()
is_test = False
day = 21
input_file = f'./input/day{day}{"_test" if is_test else ""}.txt'
# UDLR
dirs = ((-1, 0), (0, 1), (1, 0), (0, -1))
acts = [ '^', '>', 'v', '<']
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]))}, len(ls), len(ls[0])
def sol():
g, m, n = build_grid([list('789'), list('456'), list('123'), list('#0A')])
g2, _, _ = build_grid([list('#^A'), list('<v>')])
def find_path(sr, sc, er, ec, g):
parents = defaultdict(tuple)
def dijkstra(sr, sc, er, ec, g):
open_list = [(0, sr, sc, 0)]
heapify(open_list)
closed_set = set()
parents[sr, sc] = (sr, sc)
while open_list:
score, r, c, d = heappop(open_list)
if (r, c) in closed_set:
continue
closed_set.add((r, c))
if (r, c) != (sr, sc):
parents[r, c] = (r - dirs[d][0], c - dirs[d][1], acts[d])
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] != '#':
new_score = score + 1
heappush(open_list, (new_score, nr, nc, idx))
return None
def construct_path(parents, path, start_r, start_c, end_r, end_c):
r, c = end_r, end_c
while (r, c) != (start_r, start_c):
r, c, d = parents[r, c]
path.append(d)
path.reverse()
return path
dijkstra(sr, sc, er, ec, g)
path = construct_path(parents, [], sr, sc, er, ec)
return path
def find_pos(char, g):
for k, v in g.items():
if v == char:
return k
@cache
def generate(input_lst):
rr = 'A' + input_lst
res = []
for i in range(1, len(rr)):
sr, sc = find_pos(rr[i - 1], g2)
er, ec = find_pos(rr[i], g2)
path = find_path(sr, sc, er, ec, g2)
path = (''.join(path)
.replace('<v<', 'v<<')
.replace('>^>', '>>^'))
res.append(path + 'A')
return ''.join(res)
total = 0
for line in open(input_file).read().splitlines():
ss = 'A' + line
res = []
for i in range(1, len(ss)):
sr, sc = find_pos(ss[i-1], g)
er, ec = find_pos(ss[i], g)
path = find_path(sr, sc, er, ec, g)
path = (''.join(path)
# .replace('^^<<', '<<^^')
.replace('>vv>', '>>vv')
.replace('>v>v', '>>vv')
.replace('>vvv>', '>>vvv')
.replace('>v>', '>>v')
.replace('v<v', '<vv')
.replace('v<<v', '<<vv')
.replace('v>v', '>vv')
.replace('v>>v', '>>vv')
.replace('^<^', '^^<')
.replace('^<<^', '^^<<')
.replace('<^<', '^<<')
.replace('<^^^<', '^^^<<')
.replace('<^^<', '^^<<'))
res.append(path + 'A')
print(res)
for choice in res:
next_output = choice
for i in range(2):
next_output = generate(next_output)
length = len(next_output)
num = int(ss[1:4])
total += length * num
print(total)
sol()
print(f'{(time.time() - start_time) * 1000:.3f}ms')