summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2020-12-11 23:53:45 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2020-12-11 23:53:45 +0100
commit4985ee94450df0bbcf982b7652c946a47707e60c (patch)
tree6a19652db8f05ab6546f9b5fe00c7e652f8acd8e /day2/__init__.py
download2021-4985ee94450df0bbcf982b7652c946a47707e60c.tar.gz
2021-4985ee94450df0bbcf982b7652c946a47707e60c.tar.bz2
2021-4985ee94450df0bbcf982b7652c946a47707e60c.zip
Added AoC day 1 and 2
Diffstat (limited to 'day2/__init__.py')
-rw-r--r--day2/__init__.py63
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 @@
1import re
2from dataclasses import dataclass
3from typing import Generator
4
5from aoc import AssignmentBase
6
7
8matcher = re.compile(
9 pattern=r'(?P<min>\d+)-(?P<max>\d+) (?P<letter>\w): (?P<password>\w+)'
10)
11
12
13@dataclass
14class Item:
15 min: int
16 max: int
17 letter: str
18 password: str
19
20
21class 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
44class AssignmentOne(Assignment):
45 def valid_password(self, item: Item) -> bool:
46 return item.min <= item.password.count(item.letter) <= item.max
47
48
49class 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 )