-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.py
127 lines (114 loc) · 3.17 KB
/
24.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
print(chr(27)+'[2j')
print('\033c')
f = open('24.test', 'r')
f = open('24.input', 'r')
mtx = [x.strip() for x in f.readlines()]
print("Day 24")
import copy
from collections import deque
from colorama import Fore, Back, Style
start = (1,0)
end = (len(mtx[-1])-2,len(mtx)-2)
R = 0
D = 1
L = 2
U = 3
wind = [
set(),
set(),
set(),
set(),
]
for y,row in enumerate(mtx):
for x,cell in enumerate(row):
if cell == '>':
wind[R].add((x,y))
if cell == '<':
wind[L].add((x,y))
if cell == 'v':
wind[D].add((x,y))
if cell == '^':
wind[U].add((x,y))
W = len(mtx[0])-2
H = len(mtx)-2
def print_map(t, pos):
for y, row in enumerate(mtx):
line = ""
for x, cell in enumerate(row):
x2,y2 = x-1, y-1
if pos == (x,y):
line += Fore.RED + "E" + Style.RESET_ALL
continue
if 0==x or y==0 or x==len(row)-1 or y==len(mtx)-1:
line += Fore.LIGHTBLACK_EX + cell + Style.RESET_ALL
continue
if ((x2-t)%W+1,y) in wind[R]:
line += ">"
elif ((x2+t)%W+1,y) in wind[L]:
line += "<"
elif (x,(y2+t)%H+1) in wind[U]:
line += "^"
elif (x,(y2-t)%H+1) in wind[D]:
line += "v"
#elif ((x-t)%W,y) in wind[L]:
# line += "^"
#elif t%H in wind_v_b[x2]:
# line += "v"
else:
line += "."
print(line)
def walk(T, start,end):
print("WALK", start, "to", end, "with T=", T)
verbose = T != 0
verbose = False
q = deque()
visited = []
q.append((T,start, visited))
DIR = [(1,0), (-1,0), (0,1), (0,-1)]
states = set()
while len(q) > 0:
T, (x,y), visited = q.popleft()
if (T, (x,y)) in states:
continue
states.add((T, (x,y)))
#print("NXT", T, (x,y), len(q))
if verbose:
print_map(T, (x,y))
visited = [x for x in visited]
visited.append((T,(x,y)))
if (x,y) == end:
print("Solution", T+1, "for", start, end)
return T+1
#for t, (x,y) in visited:
# print('-'*20)
# print(t, (x,y))
# print_map(t, (x,y))
Ns = [(x+dx, y+dy) for dx,dy in DIR]
Ns = [(nx,ny) for nx,ny in Ns if (
0 < nx and nx < len(mtx[0])-1
and 0 < ny and ny < len(mtx)-1
)]
Ns.append((x,y))
t2 = T+1
# Go to neighbours
for (nx,ny) in Ns:
x2 = nx-1
y2 = ny-1
if ((x2-t2)%W+1,ny) in wind[R]:
continue
elif ((x2+t2)%W+1,ny) in wind[L]:
continue
elif (nx,(y2+t2)%H+1) in wind[U]:
continue
elif (nx,(y2-t2)%H+1) in wind[D]:
continue
q.append((t2,(nx,ny), visited))
t1 = 0
t2 = walk(t1, start,end)
print("Walk 1:", t2)
end2 = (len(mtx[-1])-2,len(mtx)-1)
start2= (1,1)
t3 = walk(t2, end2, start2)
print("Walk 2:", t3)
t4 = walk(t3, start,end)
print("Walk 3:", t4)