35 lines
654 B
Crystal
35 lines
654 B
Crystal
require "./common.cr"
|
|
|
|
lines = File.read("day5_input").split("\n")
|
|
|
|
struct Char
|
|
def inverse?(other)
|
|
return (other != self) && (other.downcase == self.downcase)
|
|
end
|
|
end
|
|
|
|
def react(string)
|
|
stack = [] of Char
|
|
|
|
string.each_char do |char|
|
|
last_char = stack.last?
|
|
if !last_char || !last_char.inverse? char
|
|
stack << char
|
|
else
|
|
stack.pop
|
|
end
|
|
end
|
|
|
|
return stack
|
|
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] }
|
|
pairs[pair] = react(new_string).size
|
|
end
|
|
|
|
puts pairs.values.min
|