From 125317f14645d087782d4d17b90173973ad10cdb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 1 Dec 2020 21:09:04 -0800 Subject: [PATCH] Add initial solution to day 2 --- day2.cr | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 day2.cr diff --git a/day2.cr b/day2.cr new file mode 100644 index 0000000..83986bd --- /dev/null +++ b/day2.cr @@ -0,0 +1,37 @@ +INPUT = File.read("day2.txt").lines.map(&.chomp) + +def part1 + input = INPUT.clone + total = 0 + input.each do |line| + data = line.match(/([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)/) + next unless data + min = data[1].to_i + max = data[2].to_i + char = data[3][0] + str = data[4] + count = str.chars.count(char) + total += 1 if count >= min && count <= max + end + + puts total +end + +def part2 + input = INPUT.clone + total = 0 + input.each do |line| + data = line.match(/([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)/) + next unless data + min = data[1].to_i - 1 + max = data[2].to_i - 1 + char = data[3][0] + str = data[4] + count = str.chars.count(char) + total += 1 if ((str[min] == char) || (str[max] == char) ) && str[min] != str[max] + end + puts total +end + +part1 +part2