From 4985ee94450df0bbcf982b7652c946a47707e60c Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 11 Dec 2020 23:53:45 +0100 Subject: Added AoC day 1 and 2 --- day2/__init__.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 day2/__init__.py (limited to 'day2/__init__.py') diff --git a/day2/__init__.py b/day2/__init__.py new file mode 100644 index 0000000..988c32e --- /dev/null +++ b/day2/__init__.py @@ -0,0 +1,63 @@ +import re +from dataclasses import dataclass +from typing import Generator + +from aoc import AssignmentBase + + +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(AssignmentBase): + 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 + ) + ) -- cgit v1.2.3