Add initial solution to day 2

This commit is contained in:
Danila Fedorin 2020-12-01 21:09:04 -08:00
parent aee4c67e43
commit 125317f146

37
day2.cr Normal file
View File

@ -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