Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 14, 2021 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarahzrf/3858cf36cd5140efb0b3611a444f2c2e to your computer and use it in GitHub Desktop.
Save sarahzrf/3858cf36cd5140efb0b3611a444f2c2e to your computer and use it in GitHub Desktop.
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