aoc2020/day02.py
Stefan Harmuth 5f1e68270b day2
2020-12-02 09:17:39 +01:00

28 lines
668 B
Python

import aoclib
import re
if 'TEST' not in globals():
TEST = False
my_input = aoclib.getInputLineAsArray(day=2, test=TEST)
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
def getCharacterCount(password, character):
count = 0
for char in password:
if char == character:
count = count + 1
return count
valid_count = 0
for line in my_input:
match = re.match(splitter, line)
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
if int(min_count) <= getCharacterCount(password, character) <= int(max_count):
valid_count = valid_count + 1
aoclib.printSolution(2, 1, valid_count, TEST)