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
input = ARGF.read.chomp
REGEX = Regexp.new(
(?a..?z)
.map {|x| [x, x.upcase] }
.flat_map(&:permutation)
.flat_map(&:to_a)
.map(&:join)
.join(?|)
)
def react(polymer)
last = nil
until polymer == last
last = polymer
polymer = polymer.gsub(REGEX, "")
end
polymer
end
# Part One
# p react(input).size
# Part Two
p (?a..?z)
.map {|unit| input.gsub(/#{unit}/i, "") }
.map {|polymer| react(polymer) }
.map {|reacted| reacted.size }
.min