diff options
Diffstat (limited to 'day2/__init__.py')
| -rw-r--r-- | day2/__init__.py | 63 |
1 files changed, 63 insertions, 0 deletions
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 @@ | |||
| 1 | import re | ||
| 2 | from dataclasses import dataclass | ||
| 3 | from typing import Generator | ||
| 4 | |||
| 5 | from aoc import AssignmentBase | ||
| 6 | |||
| 7 | |||
| 8 | matcher = re.compile( | ||
| 9 | pattern=r'(?P<min>\d+)-(?P<max>\d+) (?P<letter>\w): (?P<password>\w+)' | ||
| 10 | ) | ||
| 11 | |||
| 12 | |||
| 13 | @dataclass | ||
| 14 | class Item: | ||
| 15 | min: int | ||
| 16 | max: int | ||
| 17 | letter: str | ||
| 18 | password: str | ||
| 19 | |||
| 20 | |||
| 21 | class Assignment(AssignmentBase): | ||
| 22 | def parse_item(self, item: str) -> Item: | ||
| 23 | match = matcher.match(item).groupdict() | ||
| 24 | return Item( | ||
| 25 | min=int(match['min']), | ||
| 26 | max=int(match['max']), | ||
| 27 | letter=match['letter'], | ||
| 28 | password=match['password'], | ||
| 29 | ) | ||
| 30 | |||
| 31 | def valid_password(self, item: Item) -> bool: | ||
| 32 | raise NotImplementedError('Implement Valid Password') | ||
| 33 | |||
| 34 | def run(self, input: Generator): | ||
| 35 | valid_passwords = 0 | ||
| 36 | |||
| 37 | for i in input: | ||
| 38 | if self.valid_password(i): | ||
| 39 | valid_passwords += 1 | ||
| 40 | |||
| 41 | return valid_passwords | ||
| 42 | |||
| 43 | |||
| 44 | class AssignmentOne(Assignment): | ||
| 45 | def valid_password(self, item: Item) -> bool: | ||
| 46 | return item.min <= item.password.count(item.letter) <= item.max | ||
| 47 | |||
| 48 | |||
| 49 | class AssignmentTwo(Assignment): | ||
| 50 | def valid_password(self, item: Item) -> bool: | ||
| 51 | return ( | ||
| 52 | ( | ||
| 53 | item.password[item.min - 1] == item.letter | ||
| 54 | and | ||
| 55 | item.password[item.max - 1] != item.letter | ||
| 56 | ) | ||
| 57 | or | ||
| 58 | ( | ||
| 59 | item.password[item.min - 1] != item.letter | ||
| 60 | and | ||
| 61 | item.password[item.max - 1] == item.letter | ||
| 62 | ) | ||
| 63 | ) | ||
