Created
December 14, 2021 05:15
-
-
Save sarahzrf/9ceb4382172a7a971767022815191926 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 | |
10.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