summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-02 17:39:03 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-02 17:39:03 +0100
commit4dec21f362c03136e9811a4f4c162fcd8c50544e (patch)
treecd90c52c7c936fdbe5fc7f22f3f5bf3240faf9a8 /day2/__init__.py
parent37aa8eec0498d7e8491084711132f16db9129a39 (diff)
download2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.gz
2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.bz2
2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.zip
Added day 10
Diffstat (limited to 'day2/__init__.py')
-rw-r--r--day2/__init__.py63
1 files changed, 0 insertions, 63 deletions
diff --git a/day2/__init__.py b/day2/__init__.py
deleted file mode 100644
index 1d65d13..0000000
--- a/day2/__init__.py
+++ /dev/null
@@ -1,63 +0,0 @@
1import re
2from dataclasses import dataclass
3from typing import Generator
4
5from aoc import BaseAssignment
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(BaseAssignment):
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 )