Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 14, 2021 05:27
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/e038f00787b06ab34fbbc7e35ff19477 to your computer and use it in GitHub Desktop.
Save sarahzrf/e038f00787b06ab34fbbc7e35ff19477 to your computer and use it in GitHub Desktop.
def expand(rules, dat)
next_dat = Hash.new(0)
dat.each do |(l, r), ct|
m = rules[[l, r]]
next_dat[[l, m]] += ct
next_dat[[m, r]] += ct
end
next_dat
end
rules = {}
polymer = Hash.new(0)
raw_polymer = gets.chomp
last = raw_polymer[-1]
raw_polymer.chars.each_cons(2) do |adj|
polymer[adj] += 1
end
gets
$<.each do |line|
line =~ /(\w\w) -> (\w)/
rules[$1.chars] = $2
end
40.times do
polymer = expand(rules, polymer)
end
counts = Hash.new(0)
counts[last] += 1
polymer.each do |(l, r), ct|
counts[l] += ct
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