AdventOfCode-2018/day5.cr

35 lines
654 B
Crystal
Raw Normal View History

2018-12-04 21:35:58 -08:00
require "./common.cr"
lines = File.read("day5_input").split("\n")
2018-12-05 20:43:39 -08:00
struct Char
def inverse?(other)
return (other != self) && (other.downcase == self.downcase)
end
end
2018-12-04 21:35:58 -08:00
def react(string)
2018-12-05 20:43:39 -08:00
stack = [] of Char
string.each_char do |char|
last_char = stack.last?
if !last_char || !last_char.inverse? char
stack << char
else
stack.pop
2018-12-04 21:35:58 -08:00
end
end
2018-12-05 20:43:39 -08:00
return stack
2018-12-04 21:35:58 -08:00
end
puts react(lines[0]).size
pairs = {} of Tuple(Char, Char) => Int32
('a'..'z').to_a.zip(('A'..'Z').to_a).each do |pair|
new_string = lines[0].select { |c| c != pair[0] && c != pair[1] }
2018-12-04 21:35:58 -08:00
pairs[pair] = react(new_string).size
end
puts pairs.values.min