2018-12-02 21:34:51 -08:00
|
|
|
require "./common.cr"
|
|
|
|
|
2018-12-02 21:40:18 -08:00
|
|
|
lines = File.read("day2_input").split("\n")
|
2018-12-02 21:34:51 -08:00
|
|
|
lines.pop
|
|
|
|
|
|
|
|
CHARS = ('a'..'z').to_a
|
|
|
|
|
|
|
|
def has_n?(string, n)
|
|
|
|
return string.chars.count_each(CHARS).any?(&.[](1).==(n))
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_changes(s1, s2)
|
|
|
|
pairs = s1.chars.zip s2.chars
|
|
|
|
total = 0
|
|
|
|
pairs.each do |pair|
|
|
|
|
total += 1 if pair[0] != pair[1]
|
|
|
|
end
|
|
|
|
return total
|
|
|
|
end
|
|
|
|
|
|
|
|
two = lines.count { |s| has_n?(s, 2) }
|
|
|
|
three = lines.count { |s| has_n?(s, 3) }
|
|
|
|
puts "\"Hash\": #{two * three}"
|
|
|
|
|
|
|
|
lines.product(lines).each do |pair|
|
|
|
|
if count_changes(pair[0], pair[1]) == 1
|
|
|
|
puts "Strings: #{pair[0]}, #{pair[1]}"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|