|
|
@ -1,11 +1,14 @@
|
|
|
|
replacements, molecule = DATA.read.chomp.split("\n\n")
|
|
|
|
replacements, molecule = DATA.read.chomp.split("\n\n")
|
|
|
|
replacements = replacements.scan(/(\w+) \=> (\w+)/)
|
|
|
|
replacements = replacements.scan(/(\w+) \=> (\w+)/).map(&:reverse).sort_by {|k,_| -k.size}
|
|
|
|
puts replacements.flat_map {|k,v|
|
|
|
|
|
|
|
|
m = molecule.split(k, -1)
|
|
|
|
steps = 0
|
|
|
|
(0...m.size-1).map {|i|
|
|
|
|
until molecule == ?e
|
|
|
|
m[0..i].join(k) + v + m[i+1..-1].join(k)
|
|
|
|
k,v = replacements.find {|k,_| molecule.include?(k) }
|
|
|
|
}
|
|
|
|
steps += molecule.scan(/#{k}/).size
|
|
|
|
}.uniq.size
|
|
|
|
|
|
|
|
|
|
|
|
molecule.gsub!(k, v)
|
|
|
|
|
|
|
|
puts "#{steps}: #{molecule}"
|
|
|
|
|
|
|
|
end
|
|
|
|
__END__
|
|
|
|
__END__
|
|
|
|
Al => ThF
|
|
|
|
Al => ThF
|
|
|
|
Al => ThRnFAr
|
|
|
|
Al => ThRnFAr
|
|
|
|