2018-12-04 21:35:58 -08:00
|
|
|
require "./common.cr"
|
|
|
|
|
|
|
|
lines = File.read("day5_input").split("\n")
|
|
|
|
|
|
|
|
def react(string)
|
|
|
|
string = string.chars
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
while changed
|
|
|
|
changed = false
|
|
|
|
i = 0
|
|
|
|
while i < string.size - 1
|
|
|
|
next_i = i + 1
|
|
|
|
if string[i] != string[next_i] &&
|
2018-12-05 15:58:46 -08:00
|
|
|
string[i].downcase == string[next_i].downcase
|
2018-12-04 21:35:58 -08:00
|
|
|
string.delete_at i
|
|
|
|
string.delete_at i
|
|
|
|
|
|
|
|
i -= 1
|
|
|
|
i = 0 if i < 0
|
|
|
|
changed = true
|
|
|
|
else
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return string
|
|
|
|
end
|
|
|
|
|
|
|
|
puts react(lines[0]).size
|
|
|
|
|
|
|
|
pairs = {} of Tuple(Char, Char) => Int32
|
|
|
|
('a'..'z').to_a.zip(('A'..'Z').to_a).each do |pair|
|
2018-12-05 15:58:46 -08:00
|
|
|
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
|