Add solutions for day 2.
This commit is contained in:
parent
b370a741e3
commit
fd51b9dca2
15
day2_1.cr
Normal file
15
day2_1.cr
Normal 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
15
day2_2.cr
Normal 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
|
Loading…
Reference in New Issue
Block a user