AoC2015/day19/solution.rb

52 lines
1.2 KiB
Ruby
Executable File

#!/bin/env ruby
require 'pry'
input = File.readlines('./input').map(&:chomp)
starting_molecule = input.pop.scan(/[A-Z][a-z]?/)
starting_molecule.freeze
input.pop
input.map! { |pair| pair.split('=>').map(&:strip) }
results = []
starting_molecule.each.with_index do |element, i|
input.each do |pair|
next unless pair[0] == element
molecule = starting_molecule.dup
molecule[i] = pair[1]
results.push(molecule.reduce(&:+))
end
end
puts "Part 1 Solution: #{results.uniq.count}"
# binding.pry
result = starting_molecule.reduce(&:+)
count = 0
counts = []
100.times do
until result == 'e'
previous = result.dup
matches = input.map do |pair|
# pair[1].scan(/[A-Z]/).size * result.scan(pair[1]).size
result.scan(pair[1]).size
end
match = matches.find_index(matches.reject(&:zero?).sample)
result.gsub!(input[match][1], input[match][0]) unless match.nil?
count += matches[match] unless match.nil?
next unless previous == result
input.shuffle!
count == 0
result = starting_molecule.reduce(&:+)
# break if gets.chomp == 'q'
end
result = starting_molecule.reduce(&:+)
input.shuffle!
counts.push(count)
count = 0
end
puts "Part 2 Solution: #{counts.min}"