-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (110 loc) · 3.85 KB
/
main.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
import collections
import math
with open('input.txt') as file:
LINES = file.read().strip().splitlines()
total = 0
broadcaster_dests = []
flipflops = {}
conjunctions = {}
for line in LINES:
source, dests = line.split(' -> ')
dests = dests.split(', ')
if source == 'broadcaster':
broadcaster_dests.extend(dests)
else:
prefix = source[0]
if prefix == '%':
flipflops[source[1:]] = [dests, 0]
elif prefix == '&':
conjunctions[source[1:]] = [dests, {}]
for line in LINES:
source, dests = line.split(' -> ')
dests = dests.split(', ')
for dest in dests:
if dest in conjunctions:
conjunctions[dest][1][source[1:]] = 0
tot = [0, 0]
for i in range(1000):
queue = collections.deque()
queue.append(('broadcaster', broadcaster_dests, 0))
tot[0] += len(broadcaster_dests) + 1
while queue:
source, dests, pulse_type = queue.popleft()
for dest in dests:
if dest in flipflops:
if pulse_type == 0:
flipflops_state = flipflops[dest][1]
if flipflops_state == 0:
flipflops[dest][1] = 1
else:
flipflops[dest][1] = 0
send, state = flipflops[dest]
tot[state] += len(send)
queue.append((dest, send, state))
elif dest in conjunctions:
conjunctions[dest][1][source] = pulse_type
send = conjunctions[dest][0]
state = 0 if all(conjunctions[dest][1].values()) else 1
tot[state] += len(send)
queue.append((dest, send, state))
total = math.prod(tot)
print(total)
total = 0
broadcaster_dests = []
flipflops = {}
conjunctions = {}
rx_source = None
for line in LINES:
source, dests = line.split(' -> ')
dests = dests.split(', ')
if dests[0] == 'rx':
rx_source = source[1:]
if source == 'broadcaster':
broadcaster_dests.extend(dests)
else:
prefix = source[0]
if prefix == '%':
flipflops[source[1:]] = [dests, 0]
elif prefix == '&':
conjunctions[source[1:]] = [dests, {}]
__dests = []
for line in LINES:
source, dests = line.split(' -> ')
dests = dests.split(', ')
if rx_source in dests:
__dests.append(source[1:])
for dest in dests:
if dest in conjunctions:
conjunctions[dest][1][source[1:]] = 0
tot = [0, 0]
patterns = [0 for _ in range(len(__dests))]
i = 0
while all([x != 0 for x in patterns]) is False:
queue = collections.deque()
queue.append(('broadcaster', broadcaster_dests, 0))
tot[0] += len(broadcaster_dests) + 1
while queue:
source, dests, pulse_type = queue.popleft()
for dest in dests:
if dest in flipflops:
if pulse_type == 0:
flipflops_state = flipflops[dest][1]
if flipflops_state == 0:
flipflops[dest][1] = 1
else:
flipflops[dest][1] = 0
send, state = flipflops[dest]
if i <= 1000:
tot[state] += len(send)
queue.append((dest, send, state))
elif dest in conjunctions:
conjunctions[dest][1][source] = pulse_type
send = conjunctions[dest][0]
state = 0 if all(conjunctions[dest][1].values()) else 1
if i <= 1000:
tot[state] += len(send)
queue.append((dest, send, state))
if dest in __dests and state == 1:
patterns[__dests.index(dest)] = i + 1
i += 1
print(math.lcm(*patterns))