-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.py
152 lines (141 loc) · 4.69 KB
/
20.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import collections
import heapq
maze = []
portals = {}
def add_portal(name, location):
y, x = location
if name in portals:
old = portals.pop(name)
portals[location] = old
portals[old] = location
else:
portals[name] = location
def make_maze():
with open('../20.txt') as f:
first = None
second = None
seen = {}
for i, line in enumerate(f, start=-2):
length = len(line) - 5
if line[2] == '#' or line[2] == '.':
maze.append([])
for j, c in enumerate(line[:-1], start = -2):
if c == '#' or c == '.':
maze[-1].append(c)
elif c == ' ':
if 0 <= j < length:
maze[-1].append(c)
elif (i, j-1) in seen:
if 0 <= j < length:
maze[-1].append(' ')
name = seen.pop((i, j-1)) + c
if line[j] != '.':
add_portal(name, (i, j+1))
else:
add_portal(name, (i, j-2))
elif (i-1, j) in seen:
maze[-1].append(' ')
name = seen.pop((i-1, j)) + c
if maze[i-2][j] == '.':
add_portal(name, (i-2, j))
else:
add_portal(name, (i+1, j))
else:
if 0 <= j < length:
maze[-1].append(' ')
seen[(i, j)] = c
else:
if first is None:
first = line[:-1]
else:
second = line[:-1]
for j, (c1, c2) in enumerate(zip(first, second), start = -2):
if c1 != ' ':
name = c1 + c2
if i == -1:
target = 0
else:
target = i-2
add_portal(name, (target, j))
first = None
second = None
make_maze()
height = len(maze)
width = len(maze[0])
def neighbors(location):
y, x = location
if y > 0:
yield (y-1, x)
if y < height-1:
yield (y+1, x)
if x > 0:
yield (y, x-1)
if x < width-1:
yield (y, x+1)
connections = {}
def build_connections():
vals = portals.values()
for node in vals:
subcon = {}
try:
oy, ox = node
if oy == 0 or ox == 0 or oy == height-1 or ox == width-1:
level_change = -1
else:
level_change = 1
subcon[portals[node]] = (1, level_change)
except KeyError:
pass
to_check = collections.deque([(0, node)])
seen = set()
try:
while True:
distance, location = to_check.popleft()
if location in seen:
continue
seen.add(location)
if location != node and location in vals:
subcon[location] = (distance, 0)
for neighbor in neighbors(location):
if maze[neighbor[0]][neighbor[1]] == '.':
to_check.append((distance+1, neighbor))
except IndexError:
pass
connections[node] = subcon
build_connections()
def bfs():
to_check = [(0, portals['AA'])]
seen = set()
end = portals['ZZ']
try:
while True:
distance, location = heapq.heappop(to_check)
if location == end:
return distance
if location in seen:
continue
seen.add(location)
for neighbor, (weight, _) in connections[location].items():
heapq.heappush(to_check, (distance+weight, neighbor))
except IndexError:
pass
def bfs2():
to_check = [(0, 0, portals['AA'])]
seen = set()
end = portals['ZZ']
try:
while True:
distance, level, location = heapq.heappop(to_check)
if level == 0 and location == end:
return distance
if (location, level) in seen:
continue
seen.add((location, level))
for neighbor, (weight, level_change) in connections[location].items():
new_level = level + level_change
if new_level >= 0:
heapq.heappush(to_check, (distance+weight, new_level, neighbor))
except IndexError:
pass
print(bfs())
print(bfs2())