-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
103 lines (83 loc) · 2.42 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
import unittest
import re
BC = 'B'
LOW = 0
HIGH = 1
def parseInput(input):
result = {}
for line in input.splitlines():
m = re.match(r'(.+) -> (.+)', line)
c = m[1][0]
name = m[1][1:]
if c == '%' or c == '&':
module = c
else:
module = BC
name = 'broadcaster'
result[name] = (module, m[2].split(', '))
return result
def add(a, b):
return (a[0] + b[0], a[1] + b[1])
def sendPulse(data, memory, module, from_module, pulse):
queue = [(from_module, pulse, module)]
result = [0, 0]
while len(queue):
from_module, pulse, module = queue.pop(0)
result[pulse] += 1
# print('{} -{}-> {}'.format(from_module, ['low', 'high'][pulse], module), '\n')
if not module in data: continue
type, out = data[module]
out_pulse = None
if type == BC:
out_pulse = LOW
if type == '%':
if pulse == LOW:
memory[module] ^= 1
out_pulse = memory[module]
if type == '&':
mem = memory[module]
mem[from_module] = pulse
out_pulse = LOW if all(mem[f] == HIGH for f in mem) else HIGH
if out_pulse != None:
for o in out:
queue.append((module, out_pulse, o))
return result
def parse1(input):
data = parseInput(input)
memory = {}
for key in data:
type, out = data[key]
if type == '%': memory[key] = LOW
if type == '&':
mem = {}
for key2 in data:
for out in data[key2][1]:
if out == key:
mem[key2] = LOW
break
memory[key] = mem
result = (0, 0)
for i in range(0, 1000):
count = sendPulse(data, memory, 'broadcaster', 'button', LOW)
result = add(result, count)
return result[0] * result[1]
def parse2(input):
return 0
class Test(unittest.TestCase):
def test_part1_1(self):
self.assertEqual(parse1("""broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a"""), 32000000)
def test_part1_2(self):
self.assertEqual(parse1("""broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output"""), 11687500)
unittest.main(exit=False)
with open('input20.txt') as file:
data = file.read()
print("Part 1:", parse1(data))
print("Part 2:", parse2(data))