-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.rb
110 lines (94 loc) · 2.49 KB
/
day14.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
require_relative "spec_helper"
class Polymerization
attr_reader :polymer
def initialize(polymer, rules)
@polymer = polymer
@rules = rules.map { |(a, b), c| [[a, b], [[a, c], [c, b]]] }.to_h
end
def step(n = 1)
pair_counts = polymer.each_cons(2).to_a.tally
n.times do
new_pair_counts = Hash.new(0)
pair_counts.each do |pair, count|
rules[pair].each do |new_pair|
new_pair_counts[new_pair] += count
end
end
pair_counts = new_pair_counts
end
@counts = Hash.new(0)
pair_counts.each do |(_, atom), count|
@counts[atom] += count
end
end
def most_common_element_count
counts.max_by { |_, count| count }.last
end
def least_common_element_count
counts.min_by { |_, count| count }.last
end
def most_common_minus_least_common_element_count
most_common_element_count - least_common_element_count
end
def to_s
polymer.join
end
private
attr_reader :rules, :counts
end
RSpec.describe "Day 14" do
let(:example) do
parse(
<<~INPUT
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C
INPUT
)
end
let(:input) { parse(File.read("day14_input.txt")) }
def parse(input)
polymer, rules = input.split("\n\n")
[
polymer.split(""),
rules.split("\n").map do |rule|
froms, to = rule.scan(/[A-Z]+/)
[froms.split(""), to]
end.to_h
]
end
specify "part 1 - example" do
p = Polymerization.new(*example).tap { _1.step(10) }
expect(p.most_common_element_count).to eql 1749
expect(p.least_common_element_count).to eql 161
expect(p.most_common_minus_least_common_element_count).to eql 1588
end
specify "part 1 - answer" do
p = Polymerization.new(*input).tap { _1.step(10) }
expect(p.most_common_minus_least_common_element_count).to eql 2375
end
specify "part 2 - example" do
p = Polymerization.new(*example).tap { _1.step(40) }
expect(p.most_common_element_count).to eql 2192039569602
expect(p.least_common_element_count).to eql 3849876073
expect(p.most_common_minus_least_common_element_count).to eql 2188189693529
end
specify "part 2 - answer" do
p = Polymerization.new(*input).tap { _1.step(40) }
expect(p.most_common_minus_least_common_element_count).to eql 1976896901756
end
end