Created
December 14, 2021 05:27
-
-
Save sarahzrf/e038f00787b06ab34fbbc7e35ff19477 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) | |
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