Add solutions for day 2.

This commit is contained in:
Danila Fedorin 2018-12-01 21:24:37 -08:00
parent b370a741e3
commit fd51b9dca2
2 changed files with 30 additions and 0 deletions

15
day2_1.cr Normal file
View File

@ -0,0 +1,15 @@
lines = File.read("day2").split("\n")
lines.pop
CHARS = ('a'..'z').to_a
def has_n?(string, n)
CHARS.each do |c|
return true if string.count(c) == n
end
return false
end
two = lines.count { |s| has_n?(s, 2) }
three = lines.count { |s| has_n?(s, 3) }
puts two * three

15
day2_2.cr Normal file
View File

@ -0,0 +1,15 @@
lines = File.read("day2").split("\n")
lines.pop
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
lines.product(lines).each do |pair|
puts "#{pair[0]}, #{pair[1]}" if count_changes(pair[0], pair[1]) == 1
end