re: AoC Day 5: Alchemical Reduction VIEW POST

VIEW FULL DISCUSSION
 

Hey, here's my solution with ruby.

DATA = File.open('input.txt')
main_data = DATA.read
char_hash = Hash.new { |h, k| h[k] = [] }

def reduce_string(string)
  loop do
    last_length = string.size
    ('a'..'z').each do |letter|
      string.gsub!("#{letter}#{letter.upcase}", '')
      string.gsub!("#{letter.upcase}#{letter}", '')
    end
    break if last_length == string.size
  end
  string.size
end

def remove_letters(string, letter)
  string = string.dup
  string.gsub!(letter, '')
  string.gsub!(letter.upcase, '')
end

('a'..'z').each do |letter|
  char_hash[letter] = reduce_string(remove_letters(main_data, letter))
end

puts reduce_string(main_data)
puts char_hash.values.min
 
code of conduct - report abuse