-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
96 lines (68 loc) · 2.58 KB
/
11.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
from lib import *
input = read_input(2016, 11)
names = {}
get_id = lambda name: names.setdefault(name, len(names))
generators = {}
microchips = {}
for i, line in enumerate(input.splitlines()):
for g in map(get_id, re.findall(r"([a-z]+) generator", line)):
generators[g] = i
for m in map(get_id, re.findall(r"([a-z]+)-compatible microchip", line)):
microchips[m] = i
generators = tuple([generators[i] for i in range(len(generators))])
microchips = tuple([microchips[i] for i in range(len(microchips))])
sub = lambda lst, i, x: lst[:i] + (x,) + lst[i + 1 :]
def is_valid(generators, microchips):
for i, e in enumerate(microchips):
if not 0 <= e < 4:
return False
if generators[i] == e:
continue
if e in generators:
return False
return True
def solve():
def add_to_queue(d, e, g, m):
g, m = map(tuple, zip(*sorted(zip(g, m))))
if is_valid(g, m):
queue.append((d, e, g, m))
queue = [(0, 0, generators, microchips)]
visited = set()
while queue:
dist, el, ge, mi = queue.pop(0)
if (el, ge, mi) in visited:
continue
visited.add((el, ge, mi))
if ge == mi == (3,) * len(ge):
return dist
for d in [-1, 1]:
if not 0 <= (e := el + d) < 4:
continue
if d == -1 and all(x >= el for x in ge + mi):
continue
for i in range(len(ge)):
if ge[i] == el:
add_to_queue(dist + 1, e, sub(ge, i, e), mi)
if mi[i] == el:
add_to_queue(dist + 1, e, ge, sub(mi, i, e))
if ge[i] == mi[i] == el:
add_to_queue(dist + 1, e, sub(ge, i, e), sub(mi, i, e))
for j in range(i + 1, len(ge)):
if ge[i] == ge[j] == el:
add_to_queue(dist + 1, e, sub(sub(ge, j, e), i, e), mi)
if mi[i] == mi[j] == el:
add_to_queue(dist + 1, e, ge, sub(sub(mi, j, e), i, e))
print(solve())
names = {}
generators = {}
microchips = {}
for i, line in enumerate(input.splitlines()):
for g in map(get_id, re.findall(r"([a-z]+) generator", line)):
generators[g] = i
for m in map(get_id, re.findall(r"([a-z]+)-compatible microchip", line)):
microchips[m] = i
for i in map(get_id, "ab"):
generators[i] = microchips[i] = 0
generators = tuple([generators[i] for i in range(len(generators))])
microchips = tuple([microchips[i] for i in range(len(microchips))])
print(solve())