-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.py
185 lines (156 loc) · 4.59 KB
/
18.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from lib import *
input = read_input(2019, 18)
KEY_INDEX = {a: i for i, a in enumerate(string.ascii_lowercase)}
def key_to_num(key):
return 1 << KEY_INDEX[key.lower()]
grid = []
pos = None
key_count = 0
graph = {}
nodes = {}
for i, line in enumerate(input.splitlines()):
for j, c in enumerate(line):
if "a" <= c <= "z":
key_count += 1
graph[c] = {}
nodes[c] = j, i
elif "A" <= c <= "Z":
graph[c] = {}
nodes[c] = j, i
elif c == "@":
pos = j, i
graph[c] = {}
nodes[c] = j, i
grid.append(line)
for src in graph:
Q = [(*nodes[src], 0)]
visited = set()
while Q:
x, y, d = Q.pop(0)
if (x, y) in visited:
continue
visited.add((x, y))
cell = grid[y][x]
if cell != src and cell in graph:
graph[src][cell] = min(d, graph[src].get(cell, 1e1337))
continue
for dx, dy in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if not (dx in range(len(grid[0])) and dy in range(len(grid))):
continue
if grid[dy][dx] == "#":
continue
Q.append((dx, dy, d + 1))
visited = set()
Q = [(0, "@", 0)]
while Q:
d, p, keys = heapq.heappop(Q)
if (p, keys) in visited:
continue
visited.add((p, keys))
if keys == (1 << key_count) - 1:
print(d)
break
j, i = nodes[p]
cell = grid[i][j]
if "a" <= cell <= "z" and not key_to_num(cell) & keys:
heapq.heappush(Q, (d, p, keys | key_to_num(cell)))
continue
for q, dist in graph[p].items():
if "A" <= q <= "Z" and not key_to_num(q.lower()) & keys:
continue
heapq.heappush(Q, (d + dist, q, keys))
grid = []
pos = None
key_count = 0
graph = {str(i): {} for i in range(4)}
nodes = {str(i): None for i in range(4)}
for i, line in enumerate(input.splitlines()):
line = list(line)
for j, c in enumerate(line):
if "a" <= c <= "z":
key_count += 1
graph[c] = {}
nodes[c] = j, i
elif "A" <= c <= "Z":
graph[c] = {}
nodes[c] = j, i
elif c == "@":
pos = j, i
graph[c] = {}
nodes[c] = j, i
grid.append(line)
sector_keys = [
set(grid[i][j] for i in range(len(grid) // 2) for j in range(len(grid[0]) // 2) if "a" <= grid[i][j] <= "z"),
set(
grid[i][j]
for i in range(len(grid) // 2)
for j in range(len(grid[0]) // 2, len(grid[0]))
if "a" <= grid[i][j] <= "z"
),
set(
grid[i][j]
for i in range(len(grid) // 2, len(grid))
for j in range(len(grid[0]) // 2)
if "a" <= grid[i][j] <= "z"
),
set(
grid[i][j]
for i in range(len(grid) // 2, len(grid))
for j in range(len(grid[0]) // 2, len(grid[0]))
if "a" <= grid[i][j] <= "z"
),
]
j, i = pos
del graph["@"]
del nodes["@"]
for i, j in [(i, j), (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
grid[i][j] = "#"
j, i = pos
grid[i - 1][j - 1] = "0"
grid[i - 1][j + 1] = "1"
grid[i + 1][j - 1] = "2"
grid[i + 1][j + 1] = "3"
nodes["0"] = j - 1, i - 1
nodes["1"] = j + 1, i - 1
nodes["2"] = j - 1, i + 1
nodes["3"] = j + 1, i + 1
for src in graph:
Q = [(*nodes[src], 0)]
visited = set()
while Q:
x, y, d = Q.pop(0)
if (x, y) in visited:
continue
visited.add((x, y))
cell = grid[y][x]
if cell != src and cell in graph:
graph[src][cell] = min(d, graph[src].get(cell, 1e1337))
continue
for dx, dy in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if not (dx in range(len(grid[0])) and dy in range(len(grid))):
continue
if grid[dy][dx] == "#":
continue
Q.append((dx, dy, d + 1))
out = 0
for robot in range(4):
visited = set()
Q = [(0, str(robot), 0)]
while Q:
d, p, keys = heapq.heappop(Q)
if (p, keys) in visited:
continue
visited.add((p, keys))
if keys == sum(key_to_num(ek) for ek in sector_keys[robot]):
out += d
break
j, i = nodes[p]
cell = grid[i][j]
if "a" <= cell <= "z" and not key_to_num(cell) & keys:
heapq.heappush(Q, (d, p, keys | key_to_num(cell)))
continue
for q, dist in graph[p].items():
if "A" <= q <= "Z" and not key_to_num(q.lower()) & keys and q in sector_keys[robot]:
continue
heapq.heappush(Q, (d + dist, q, keys))
print(out)