-
Notifications
You must be signed in to change notification settings - Fork 0
/
14a_solution.rb
52 lines (42 loc) · 884 Bytes
/
14a_solution.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
input = File.read("14input.txt").split("\n")
polymer = input[0].split("")
rules = input[2..].map do |line|
parts = line.split(" -> ")
[parts[0].split(""), parts[1]]
end.to_h
num_steps = 10
def step(polymer, rules)
polymer.each_cons(2).flat_map do |pair|
insertion = rules[pair]
if insertion.nil?
pair[0]
else
[pair[0], insertion]
end
end + [polymer[-1]]
end
def count(polymer)
freqs = Hash.new { |h, k| h[k] = 0 }
polymer.each do |c|
freqs[c] += 1
end
most_common = nil
max = nil
least_common = nil
min = nil
freqs.keys.each do |k|
if most_common.nil? || freqs[k] > max
most_common = k
max = freqs[k]
end
if least_common.nil? || freqs[k] < min
least_common = k
min = freqs[k]
end
end
max - min
end
num_steps.times do
polymer = step(polymer, rules)
end
puts count(polymer)