diff --git a/day2_1.cr b/day2_1.cr new file mode 100644 index 0000000..520988a --- /dev/null +++ b/day2_1.cr @@ -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 diff --git a/day2_2.cr b/day2_2.cr new file mode 100644 index 0000000..5e2ac53 --- /dev/null +++ b/day2_2.cr @@ -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