-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.py
88 lines (53 loc) · 1.97 KB
/
23.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
from lib import *
input = read_input(2021, 23)
def generate_moves(rooms, hallway, n):
def check_hallway(start, end):
return all(hallway[i] is None or i == start for i in range(min(start, end), max(start, end) + 1))
def push_room(idx, elem):
return rooms[:idx] + (rooms[idx] + (elem,),) + rooms[idx + 1 :]
def pop_room(idx):
return rooms[:idx] + (rooms[idx][:-1],) + rooms[idx + 1 :]
def set_hallway(idx, elem):
return hallway[:idx] + (elem,) + hallway[idx + 1 :]
for i, c in enumerate(hallway):
if c is None:
continue
dst = "ABCD".index(c)
if any(x != c for x in rooms[dst]):
continue
if not check_hallway(i, 2 + 2 * dst):
continue
dist = abs(2 + 2 * dst - i) + (n - len(rooms[dst]))
yield dist * 10**dst, push_room(dst, c), set_hallway(i, None)
return
for i in range(4):
if all(x == "ABCD"[i] for x in rooms[i]):
continue
c = rooms[i][-1]
src = 2 + 2 * i
dst = "ABCD".index(c)
for j in [0, 1, 3, 5, 7, 9, 10]:
if not check_hallway(src, j):
continue
dist = (1 + n - len(rooms[i])) + abs(src - j)
yield dist * 10**dst, pop_room(i), set_hallway(j, c)
def solve(part2):
lines = input.splitlines()[3:1:-1]
if part2:
lines.insert(1, " #D#B#A#C#")
lines.insert(2, " #D#C#B#A#")
n = len(lines)
queue = [(0, cnt := 0, tuple([*zip(*lines)][3:-1:2]), (None,) * 11)]
visited = set()
while queue:
energy, _, rooms, hallway = heappop(queue)
if (rooms, hallway) in visited:
continue
visited.add((rooms, hallway))
if rooms == tuple((c,) * n for c in "ABCD"):
return energy
for c, r, h in generate_moves(rooms, hallway, n):
cnt += 1
heappush(queue, (energy + c, cnt, r, h))
print(solve(False))
print(solve(True))