-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday20.py
executable file
·151 lines (118 loc) · 2.7 KB
/
day20.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2023, 20)
fin = advent.get_input()
lines = read_lines(fin)
def go():
q = deque([('button', 'broadcaster', False)])
nlo = nhi = 0
while q:
sender, receiver, pulse = q.popleft()
# print(sender, '-high->' if pulse else '-low->', receiver)
if not pulse:
nlo += 1
else:
nhi += 1
if receiver in flops:
if not pulse:
state = flops[receiver] = not flops[receiver]
for y in g[receiver]:
q.append((receiver, y, state))
elif receiver in conj:
conj[receiver][sender] = pulse
# print(*conj[receiver].items())
state = not all(conj[receiver].values())
for y in g[receiver]:
q.append((receiver, y, state))
else:
# assert receiver == 'broadcaster', receiver
if receiver in g:
for y in g[receiver]:
q.append((receiver, y, pulse))
return nlo, nhi
def go2():
rx_prev = None
useful = set()
for a, bs in g.items():
if bs == ['rx']:
rx_prev = a
break
assert rx_prev in conj
print(f'{rx_prev=}')
for a, bs in g.items():
if rx_prev in bs:
useful.add(a)
assert a in conj
print(f'{useful=}')
# for u in useful:
# print(u, conj[u])
for i in count(1):
q = deque([('button', 'broadcaster', False)])
while q:
sender, receiver, pulse = q.popleft()
if not pulse:
if receiver in useful:
print(receiver, i)
# stop manually LOL
if receiver in flops:
if not pulse:
state = flops[receiver] = not flops[receiver]
for y in g[receiver]:
q.append((receiver, y, state))
elif receiver in conj:
conj[receiver][sender] = pulse
state = not all(conj[receiver].values())
for y in g[receiver]:
q.append((receiver, y, state))
else:
if receiver in g:
for y in g[receiver]:
q.append((receiver, y, pulse))
flops = {}
conj = {}
g = {}
for line in lines:
a, b = line.split('->')
a = a.strip()
b = list(map(str.strip, b.strip().split(',')))
# print(a, b)
if a[0] == '%':
flops[a[1:]] = False
g[a[1:]] = b
elif a[0] == '&':
conj[a[1:]] = {}
g[a[1:]] = b
for bb in b:
if bb in conj:
conj[bb][a[1:]] = False
else:
assert a == 'broadcaster'
g[a] = b
for a, bs in g.items():
for b in bs:
if b in conj:
conj[b][a] = False
orig_flops = deepcopy(flops)
orig_conj = deepcopy(conj)
tota = totb = 0
for _ in range(1000):
a, b = go()
tota += a
totb += b
ans1 = tota * totb
advent.print_answer(1, ans1)
flops = orig_flops
conj = orig_conj
# go2()
#
# Run go2() and stop manually, grab the first 4 values of the "useful" modules:
#
# useful={'vk', 'dl', 'pm', 'ks'}
# dl 3769 <---
# pm 3833 <---
# vk 3877 <---
# ks 3917 <---
# dl 7538
# ...
ans2 = lcm(3769, 3833, 3877, 3917)
advent.print_answer(2, ans2)