-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.py
166 lines (154 loc) · 4.1 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
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
158
159
160
161
162
163
164
165
166
import queue
from collections import defaultdict
from util import *
from util import Grid as g
import itertools as it
import re
h = Helper(test_mode=False, test_input="""
broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output
""")
# pulses_queue = queue.Queue()
#
#
# class SolutionFoundException(Exception):
# pass
#
#
# class Module:
# def __init__(self, name: str, link_strs: t.List[str]):
# self.name: str = name
# self.link_strs: t.List[str] = link_strs
# self.link_objects: t.List[Module] = []
#
# def init_link_objects(self, all_modules):
# for li in self.link_strs:
# try:
# self.link_objects.append(all_modules[li])
# except KeyError:
# pass
#
# def send_pulse(self, is_high_pulse: bool):
# global pulses_queue
#
# for li in self.link_objects:
# if li.name == "kj" and is_high_pulse:
# raise SolutionFoundException
# pulses_queue.put((li, is_high_pulse, self.name))
#
# def activate(self, is_high_pulse: bool, activator_name: str):
# raise NotImplemented()
#
#
# class FlipFlop(Module):
# def __init__(self, name: str, link_strs: t.List[str]):
# super().__init__(name, link_strs)
# self.state = False
#
# def activate(self, is_high_pulse: bool, activator_name):
# if is_high_pulse:
# return
#
# self.state = not self.state
# self.send_pulse(self.state)
#
#
# class Conjunction(Module):
# def __init__(self, name: str, link_strs: t.List[str]):
# super().__init__(name, link_strs)
# self.input_states = {}
#
# def init_link_objects(self, all_modules):
# super().init_link_objects(all_modules)
# for mod in all_modules.values():
# if self.name in mod.link_strs:
# self.input_states[mod.name] = False
#
# def activate(self, is_high_pulse: bool, activator_name: str):
# self.input_states[activator_name] = is_high_pulse
# self.send_pulse(not all(self.input_states.values()))
#
#
# class Broadcast(Module):
# def activate(self, is_high_pulse: bool, activator_name: str):
# self.send_pulse(is_high_pulse)
#
#
# class Button(Module):
# def activate(self, is_high_pulse: bool, activator_name: str):
# self.send_pulse(False)
#
#
# TYPE_CODES = {
# '%': FlipFlop,
# '&': Conjunction,
# 'b': Broadcast,
# }
#
# inp = h.get_input_list()
#
# modules: t.Dict[str, Module] = {}
#
# for line in inp:
# type_code = line[0]
# module_type = TYPE_CODES[type_code]
# name = line.split(' -> ')[0].strip('%&')
# links = line.split(' -> ')[1].split(', ')
# modules[name] = module_type(name, links)
#
# modules["button"] = (Button("button", ["broadcaster"]))
#
# for m in modules.values():
# m.init_link_objects(modules)
#
# step = 0
# last_high = 0
#
# while True:
# states = [str(int(m.state)) for m in modules.values() if isinstance(m, FlipFlop)]
# # print("".join(states))
#
# step += 1
# modules["pd"].activate(False, "")
#
# while not pulses_queue.empty():
# pulse = pulses_queue.get(block=False)
# # print(pulse, pulse[0].name)
# try:
# pulse[0].activate(pulse[1], pulse[2])
# except SolutionFoundException:
# print(step, step - last_high)
# last_high = step
# break
high_steps = defaultdict(lambda: 0)
steps = [
4003,
3989,
3863,
3943,
]
periods = [
3910,
3882,
3630,
3790,
]
"""
kt: goes high on step 4003 and 4004, repeats for 2 steps with period 3910 steps
xv: goes high on step 3989 and 3990, repeats for 2 steps with period 3882 steps
rg: goes high on step 3863 and 3864, repeats for 2 steps with period 3630 steps
pd: goes high on step 3943 and 3944, repeats for 2 steps with period 3790 steps
"""
while True:
for i, p in enumerate(periods):
s = steps[i]
high_steps[s] += 1
if high_steps[s] == 4:
h.submit(s)
high_steps[s + 1] += 1
if high_steps[s + 1] == 4:
h.submit(s + 1)
steps[i] += p