aoc2020/day02.py

32 lines
959 B
Python
Executable File

import aoclib
import re
DAY = 2
TEST_SOLUTION_PART1 = 2
TEST_SOLUTION_PART2 = 1
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
def part1(test_mode=False):
my_input = aoclib.getInputAsArray(day=2, test=test_mode)
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) <= password.count(character) <= int(max_count):
valid_count = valid_count + 1
return valid_count
def part2(test_mode=False):
my_input = aoclib.getInputAsArray(day=2, test=test_mode)
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 (password[int(min_count) - 1] == character) ^ (password[int(max_count) - 1] == character):
valid_count = valid_count + 1
return valid_count