Created
December 14, 2021 05:15
-
-
Save sarahzrf/3858cf36cd5140efb0b3611a444f2c2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def expand(rules, dat) | |
return to_enum(__method__, rules, dat) unless block_given? | |
dat.each_cons(2).each do |c| | |
yield c[0] | |
yield rules[c] | |
end | |
yield dat[-1] | |
end | |
rules = {} | |
polymer = gets.chomp.chars | |
gets | |
$<.each do |line| | |
line =~ /(\w\w) -> (\w)/ | |
rules[$1.chars] = $2 | |
end | |
40.times do | |
polymer = expand(rules, polymer).to_a | |
end | |
counts = Hash.new(0) | |
polymer.each do |c| | |
counts[c] += 1 | |
end | |
puts(counts.max_by(&:last)[1] - counts.min_by(&:last)[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment