re: AoC Day 5: Alchemical Reduction VIEW POST

VIEW FULL DISCUSSION
 

Ruby solution for parts 1 & 2. Nothing special here, like for many others my version for part 1 worked but was too slow for part 2, so I eventually ended up with a stack:

polymer = File.read('input.txt').chomp

def react(polymer)
  polymer.each_char.each_with_object([]) do |current, stack|
    last = stack.last
    last && current == last.swapcase ? stack.pop : stack.append(current)
  end
end

puts react(polymer).size

puts (?a..?z).map { |l| react(polymer.gsub(/#{l}/i, "")).size }.min

code of conduct - report abuse