Add day 5 solution.

This commit is contained in:
Danila Fedorin 2018-12-04 21:35:58 -08:00
parent e6a76e428f
commit a24b176417

42
day5.cr Normal file
View File

@ -0,0 +1,42 @@
require "./common.cr"
lines = File.read("day5_input").split("\n")
def react(string)
lower = string.downcase.chars
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] &&
lower[i] == lower[next_i]
string.delete_at i
string.delete_at i
lower.delete_at i
lower.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|
new_string = lines[0].delete(pair[0]).delete(pair[1])
pairs[pair] = react(new_string).size
end
puts pairs.values.min