-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20.py
More file actions
157 lines (119 loc) · 4.29 KB
/
Copy pathday20.py
File metadata and controls
157 lines (119 loc) · 4.29 KB
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
152
153
154
155
156
157
file = open("input.txt")
lines = file.read().splitlines()
class Network:
def __init__(self):
self.modules = {}
self.button = None
self.queue: list[Signal] = []
self.sended = [0, 0]
def tick(self, watch: bool = False):
output = self.get_module("rx")
while self.queue:
signal = self.queue.pop(0)
if watch and signal.destination == output and not signal.state:
return True
signal.destination.receive(signal)
return False
def create_button(self) -> "Module":
self.button = Button("button", self)
self.button.add_destination(self.get_module("broadcaster"))
return self.button
def add_module(self, module: "Module"):
self.modules[module.name] = module
def get_module(self, name: str) -> "Module":
module = self.modules.get(name)
if module is None:
module = Dummy(name, self)
self.add_module(module)
return module
class Signal:
def __init__(self, state: bool, source: "Module", destination: "Module"):
self.state = state
self.source = source
self.destination = destination
class Module:
def __init__(self, name: str, network: Network):
self.name = name
self.network = network
self.destinations = []
def add_destination(self, destination: "Module"):
self.destinations.append(destination)
def receive(self, signal: Signal):
pass
def send(self, state: bool):
for destination in self.destinations:
signal = Signal(state, self, destination)
self.network.sended[int(state)] += 1
# print("%s -%s-> %s" % (self.name, "high" if state else "low", destination.name))
self.network.queue.append(signal)
class FlipFlop(Module):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.state = False
def receive(self, signal: Signal):
if not signal.state:
self.state = not self.state
self.send(self.state)
class Conjunction(Module):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.memory = {}
def add_destination(self, destination: Module):
super().add_destination(destination)
def add_source(self, source: Module):
self.memory[source] = False
def receive(self, signal: Signal):
self.memory[signal.source] = signal.state
if all(self.memory.values()):
self.send(False)
else:
self.send(True)
class Broadcast(Module):
def receive(self, signal: Signal):
self.send(signal.state)
class Button(Module):
def press(self):
self.send(False)
return self.network.tick()
class Dummy(Module):
def receive(self, signal: Signal):
pass
def create_network():
network = Network()
for line in lines: # creating modules
name = line.split(" -> ")[0]
if name == "broadcaster":
module = Broadcast(name, network)
symbol, name = name[0], name[1:]
if symbol == "%":
module = FlipFlop(name, network)
if symbol == "&":
module = Conjunction(name, network)
network.add_module(module)
for line in lines: # connecting modules
source, destinations = line.lstrip("%&").split(" -> ")
source = network.get_module(source)
for destination in destinations.split(", "):
destination = network.get_module(destination)
source.add_destination(destination)
if isinstance(destination, Conjunction):
destination.add_source(source)
network.create_button()
return network
def part1():
network = create_network()
for _ in range(1000):
network.button.press()
return network.sended[0] * network.sended[1]
def part2(): # too slow!
network = create_network()
counter = 0
while True:
rx_received = network.button.press()
counter += 1
if rx_received:
break
return counter
print("Part 1:", part1())
print("Part 2:", part2())
file.close()