AdventOfCode-2020/day2.cr

41 lines
755 B
Crystal
Raw Normal View History

2020-12-01 21:09:04 -08:00
INPUT = File.read("day2.txt").lines.map(&.chomp)
2020-12-01 21:13:43 -08:00
def parse(line)
data = line.match(/([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)/)
data.try do |data|
min = data[1].to_i
max = data[2].to_i
char = data[3][0]
str = data[4]
{min,max,char,str}
end
end
2020-12-01 21:09:04 -08:00
def part1
input = INPUT.clone
2020-12-01 21:13:43 -08:00
total = input.count do |line|
next false unless data = parse(line)
min, max, char, str = data
2020-12-01 21:09:04 -08:00
count = str.chars.count(char)
2020-12-01 21:13:43 -08:00
count >= min && count <= max
2020-12-01 21:09:04 -08:00
end
puts total
end
def part2
input = INPUT.clone
2020-12-01 21:13:43 -08:00
total = input.count do |line|
next false unless data = parse(line)
min, max, char, str = data
min -= 1
max -= 1
2020-12-01 21:45:41 -08:00
(str[min] == char) ^ (str[max] == char)
2020-12-01 21:09:04 -08:00
end
2020-12-01 21:13:43 -08:00
2020-12-01 21:09:04 -08:00
puts total
end
part1
part2