import re from dataclasses import dataclass from typing import Generator from aoc import BaseAssignment matcher = re.compile( pattern=r'(?P\d+)-(?P\d+) (?P\w): (?P\w+)' ) @dataclass class Item: min: int max: int letter: str password: str class Assignment(BaseAssignment): def parse_item(self, item: str) -> Item: match = matcher.match(item).groupdict() return Item( min=int(match['min']), max=int(match['max']), letter=match['letter'], password=match['password'], ) def valid_password(self, item: Item) -> bool: raise NotImplementedError('Implement Valid Password') def run(self, input: Generator): valid_passwords = 0 for i in input: if self.valid_password(i): valid_passwords += 1 return valid_passwords class AssignmentOne(Assignment): def valid_password(self, item: Item) -> bool: return item.min <= item.password.count(item.letter) <= item.max class AssignmentTwo(Assignment): def valid_password(self, item: Item) -> bool: return ( ( item.password[item.min - 1] == item.letter and item.password[item.max - 1] != item.letter ) or ( item.password[item.min - 1] != item.letter and item.password[item.max - 1] == item.letter ) )