blob: 988c32e06054688eb5ee82709154e33785c0f644 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import re
from dataclasses import dataclass
from typing import Generator
from aoc import AssignmentBase
matcher = re.compile(
pattern=r'(?P<min>\d+)-(?P<max>\d+) (?P<letter>\w): (?P<password>\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
)
)
|