-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
217 lines (160 loc) · 3.95 KB
/
main.rb
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
class Pulse
attr_reader :from
attr_reader :to
attr_reader :high
def initialize(from, to, high)
@from = from
@to = to
@high = high
end
end
class Broadcaster
attr_reader :name
attr_reader :highPulseCount
attr_reader :lowPulseCount
attr_reader :destinations
attr_reader :inputs
def initialize(name)
@name = name
@destinations = []
@highPulseCount = 0
@lowPulseCount = 0
@inputs = []
end
def addDestination(destination)
@destinations << destination
end
def addInput(input)
@inputs << input
end
def process(input, high)
@destinations.map do |destination|
Pulse.new(self, destination, high)
end
end
end
class FlipFlop < Broadcaster
def initialize(name)
super
@state = false
end
def process(input, high)
if high
return []
end
@state = ! @state
super(input, @state)
end
end
class Conjunction < Broadcaster
def initialize(name)
super
@input_states = {}
end
def addInput(input)
super
@input_states[input.name] = false
end
def process(input, high)
@input_states[input.name] = high
# puts @input_states
all_high = ! @input_states.values.include?(false)
super(self, ! all_high)
end
end
def parse(file)
modules = {}
destinationNames = {}
lines = File.open(file, 'r').readlines
lines.each do |line|
splitted = line.split(' -> ')
if splitted[0] == 'broadcaster'
m = Broadcaster.new(splitted[0])
elsif splitted[0][0] == '%'
m = FlipFlop.new(splitted[0][1..])
else
m = Conjunction.new(splitted[0][1..])
end
destinationNames[m.name] = splitted[1].strip.split(', ')
modules[m.name] = m
end
modules.values.each do |m|
destinationNames[m.name].each do |destinationName|
dest = modules[destinationName]
if dest == nil
dest = Broadcaster.new(destinationName)
modules[destinationName] = dest
end
m.addDestination(dest)
dest.addInput(m)
end
end
return modules
end
def solve(modules)
sumHighPulses = 0
sumLowPulses = 0
broadcaster = modules['broadcaster']
button = Broadcaster.new("button")
button.addDestination(broadcaster)
1000.times.each do |i|
pulses = button.process(broadcaster, false)
# puts "button ==="
while ! pulses.empty?
pulse, *rest = pulses
# puts "#{pulse.from.name} - #{pulse.high} > #{pulse.to == nil ? "nil" : pulse.to.name}"
if pulse.high
sumHighPulses += 1
else
sumLowPulses += 1
end
if pulse.to != nil
newPulses = pulse.to.process(pulse.from, pulse.high)
else
newPulses = []
end
pulses = rest.concat(newPulses)
end
end
modules.values.each do |m|
sumHighPulses += m.highPulseCount
sumLowPulses += m.lowPulseCount
end
puts (sumHighPulses * sumLowPulses)
end
def getButtonPressesUntilHigh(modules, mname)
broadcaster = modules['broadcaster']
button = Broadcaster.new("button")
button.addDestination(broadcaster)
buttonPresses = 0;
while true
pulses = button.process(broadcaster, false)
buttonPresses += 1
while ! pulses.empty?
pulse, *rest = pulses
if pulse.high
if pulse.from.name == mname
return buttonPresses
end
end
newPulses = pulse.to.process(pulse.from, pulse.high)
pulses = rest.concat(newPulses)
end
end
puts buttonPresses
end
def solve2(filename)
modules = parse(filename)
# this is actually a solution crafted to the input and not a general solution.
rx = modules['rx']
n = rx.inputs[0]
buttonPresses = []
n.inputs.each do |i|
freshModules = parse(filename)
buttonPresses << getButtonPressesUntilHigh(freshModules, i.name)
end
puts buttonPresses.reduce(:lcm)
end
filename = "input"
solve(parse(filename))
solve2(filename)