-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20part1.rb
63 lines (61 loc) · 1.8 KB
/
day20part1.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
file = File.open "day20in.txt"
mappings = {}
file.each_line do |l|
(left, right) = l.chomp.split("->").map(&:strip)
if left == "broadcaster"
mappings[left] = { to: right.split(", ").map(&:strip), type: :broadcaster }
elsif left.start_with? "%"
mappings[left[1..]] = { to: right.split(", ").map(&:strip), pulse: :low, type: :flip_flop }
else
mappings[left[1..]] = { to: right.split(", ").map(&:strip), type: :conjunction, inputs: {} }
end
end
file.close
mappings.each do |k, v|
if v[:type] == :conjunction
mappings.each do |k2, v2|
if v2[:to].include? k
v[:inputs][k2] = :low
end
end
end
end
highs = 0
lows = 0
1000.times do
steps = [{ from: "button", destination: "broadcaster", pulse: :low }]
until steps.empty?
step = steps.shift
lows += 1 if step[:pulse] == :low
highs += 1 if step[:pulse] == :high
mapping = mappings[step[:destination]]
mapping = {} if mapping.nil?
if mapping[:type] == :broadcaster
mapping[:to].each do |to|
steps.push({ from: step[:destination], destination: to, pulse: step[:pulse] })
end
elsif mapping[:type] == :flip_flop
if step[:pulse] == :low
if mapping[:pulse] == :high
mapping[:pulse] = :low
else
mapping[:pulse] = :high
end
mapping[:to].each do |to|
steps.push({ from: step[:destination], destination: to, pulse: mapping[:pulse] })
end
end
elsif mapping[:type] == :conjunction
from = step[:from]
mapping[:inputs][from] = step[:pulse]
output = :high
output = :low if mapping[:inputs].all? { |kv| kv[1] == :high }
mapping[:to].each do |to|
steps.push({ from: step[:destination], destination: to, pulse: output })
end
else
print ""
end
end
end
puts highs * lows