-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.py
executable file
·121 lines (102 loc) · 2.64 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
#!/usr/bin/env pypy3
# 99/794
from util import *
if len(sys.argv) == 1:
sys.stdin = open(__file__.replace("py", "in"))
res = 0
kind = {}
ADJ = {}
for l in lines():
name, adj = l.split(" -> ")
if name[0] in "%&":
k = name[0]
name = name[1:]
else:
k = "b"
kind[name] = k
ADJ[name] = adj.split(", ")
state = {n: False for n in kind}
cst = {n: {} for n in kind if kind[n] == "&"}
for name, a in ADJ.items():
for b in a:
if kind.get(b) == "&":
cst[b][name] = False
# for name, d in cst.items():
# print(len(d), name, d)
RADJ = defaultdict(list)
for a, b in ADJ.items():
for c in b:
RADJ[c].append(a)
GC = RADJ["zh"]
# _, RV, _ = bfs(RADJ, "rx")
# print(f"{RV=}")
# exit()
T = [0, 0]
prevstate = dict(state)
ST = defaultdict(list)
nums = {}
valid = set()
for press in range(2**12):
# if press % (10**4) == 0:
# print(press)
# prevc = {n: dict(cst[n]) for n in cst}
for a, b in state.items():
if kind[a] == "%":
ST[a].append(int(b))
Q = [("", "broadcaster", False)]
for frm, name, typ in Q:
T[typ] += 1
if name == "rx" and not typ:
prints(press+1)
exit()
k = kind.get(name, "output")
if name == "broadcaster":
for a in ADJ[name]:
Q.append((name, a, typ))
elif k == "%":
if not typ:
s = not state[name]
state[name] = s
for a in ADJ[name]:
Q.append((name, a, s))
elif k == "&":
# assert kind[name] == "&", (name, kind[name])
d = cst[name]
d[frm] = typ
s = not all(d.values())
for a in ADJ[name]:
Q.append((name, a, s))
for name, _, typ in Q:
if name in GC and typ:
if name not in nums:
nums[name] = press+1
else:
assert (press+1) % nums[name] == 0
assert len(nums) == 4
prints(math.prod(nums.values()))
# print(press, {a: int(b) for a, b in state.items() if kind[a] == "%"})
"""
LS = []
for a, l in ST.items():
s = "".join(map(str, l))
for pw in range(1, 100):
xx = 2 ** pw
lenl = len(l)
if xx < lenl and 1 in l[:xx] and s == s[:xx] * (lenl // xx):
LS.append((pw, a))
break
else:
print("NO", a, state[a])
# LS.append(("".join(map(str, l)), a))
LS.sort()
for a, b in LS:
print(a, b)
"""
# print(ST)
# for a, b in prevstate.items():
# if b != state[a]:
# print(f"{press=} {a=} {b=} {state[a]=}")
# print(cst)
# a, b = T
# print(T)
# prints(a * b)