-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2224.py
71 lines (60 loc) · 1.95 KB
/
aoc2224.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
fd = 0#1
from typing import List, Tuple
def main():
file = open('./24.' + str(fd))
a = []
for line in file:
a.append(line.strip())
R = len(a)
C = len(a[0])
pos_begin = (0, 1)
pos_end = (R - 1, C - 2)
res = BFS(a, pos_begin, pos_end, 0)
res2 = BFS(a, pos_end, pos_begin, res)
res2 = BFS(a, pos_begin, pos_end, res2)
print('Star 1:', res)
print('Star 2:', res2)
# each minute you can move up, down, left, or right, or just wait
D = [ [-1,0], [0, -1], [1, 0], [0, 1], [0, 0] ]
def BFS(a: List[str], pos_begin: Tuple[int, ...], pos_end: Tuple[int, ...], time: int) -> int:
R = len(a)
C = len(a[0])
seen = set()
seen.add((pos_begin, time))
dque = []
dque.append((0, (pos_begin, time)))
r_end, c_end = pos_end
while dque:
node = dque.pop(0)
pos_curr, time = node[1]
if pos_curr == pos_end:
return time
time += 1
r, c = pos_curr
for dr, dc in D:
rr = r + dr
cc = c + dc
if cc < 1 or cc > C - 1 - 1:
continue
if rr < 0 or rr > R - 1:
continue
if rr == 0 and a[rr][cc] != '.':
continue
if rr == R - 1 and a[rr][cc] != '.':
continue
if a[rr][ (cc - 1 + time) % (C - 2) + 1 ] == '<':
continue
if a[rr][ (cc - 1 - time) % (C - 2) + 1 ] == '>':
continue
if a[(rr - 1 + time) % (R - 2) + 1][cc] == '^':
continue
if a[(rr - 1 - time) % (R - 2) + 1][cc] == 'v':
continue
state = ( (rr, cc), time )
# print(state[1], state)
if state not in seen:
seen.add(state)
dque.append( (time + abs(rr - r_end) + abs(cc - c_end), state) )
return time
if __name__ == '__main__':
main()