-
Notifications
You must be signed in to change notification settings - Fork 0
/
2320_unfinished.py
98 lines (87 loc) · 2.71 KB
/
2320_unfinished.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
F = 0
#//////////////////////////////////////////////////
#// part 2 pending //
#//////////////////////////////////////////////////
r1, r2 = 0, 0
lines = []
import math
with open('20.' + str(F)) as file:
for line in file:lines.append(line.strip())
def dbg( Thing, *names ):
if names:
for name in names: print(name, end=' ')
if isinstance(Thing, str): print(Thing)
if isinstance(Thing, dict):
Dict = Thing
print(Dict)
for k,v in Dict.items(): print(k,v)
if isinstance(Thing, list):
things = Thing
print(things)
for thing in things: print( thing )
print()
BC = [] # broadcaster
FF,CJ= {},{} # Flip-flop % - Conjunction &
for line in lines:
key, val = line.split(' -> ')
val = val.split(', ')
print(key,val)#,'from/',line)
if 'broadcast' in key:
BC = val
symbol, key = key[0], key[1:]
if symbol == '%':
FF[ key ] = val
if symbol == '&':
CJ[ key ] = val
FFCJ = FF | CJ # dict(ff, **cj)
dbg( BC, 'BC/'),dbg(FF,'FF/'),dbg(CJ,'CJ/')
dbg( FFCJ, 'FFCJ/' )
T = 1000
sent_signals = [T, 0]
CJ_find_wayback = dict()
for conj in CJ:
CJ_find_wayback[ conj ] = {}
for k, v in FFCJ.items():
if conj in v:
CJ_find_wayback[ conj ][k] = 0# = {k: 0}
dbg(CJ_find_wayback, 'CJ_find_wayback')
CJ_with_pulse = []#{}
for _ in range( T ):
TODO = [('broadcaster', 0, signal) for signal in BC]#['broadcaster']]
#dbg(TODO)
while TODO:
src, pulse, curr = TODO.pop(0)
# print('--->',pulse,'<---')
#print(src, curr)
sent_signals[pulse] += 1
if curr not in FFCJ:
continue
destlist = FF[curr] if curr in FF else CJ[curr]
if curr in FF and pulse == 0:
for dest in destlist:
job = None
if curr in CJ_with_pulse:
job = ( curr, 0, dest )
else:
job = ( curr, 1, dest )
TODO.append( job )
if curr in CJ:
CJ_find_wayback[curr][src] = pulse
if pulse == 1 and len(CJ_find_wayback[curr]) == 1:
next_pulse = 0
elif all(v for v in CJ_find_wayback[curr].values()):
next_pulse = 0
else:
next_pulse = 1
for dest in destlist:
job = ( curr, next_pulse, dest )
TODO.append(job)
if src in FF:
if pulse == 1:#pulse:#pulse == 0:#pulse:
CJ_with_pulse.append( src )
else:
CJ_with_pulse.remove( src )
#dbg (CJ_with_pulse, 'cj pulse count/')
r1 = math.prod(sent_signals)
print("part 1:", r1)
print("part 2:", r2)