summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--day4/__init__.py56
1 files changed, 54 insertions, 2 deletions
diff --git a/day4/__init__.py b/day4/__init__.py
index f179985..89f697c 100644
--- a/day4/__init__.py
+++ b/day4/__init__.py
@@ -1,8 +1,18 @@
1import re
1from dataclasses import dataclass 2from dataclasses import dataclass
2from typing import Generator, List 3from typing import Generator, List
3 4
4from aoc import BaseAssignment 5from aoc import BaseAssignment
5 6
7hgt_matcher = re.compile('(?P<value>\d+)(?P<unit>(in|cm))')
8valid_units = {
9 'cm': {'min': 150, 'max': 193},
10 'in': {'min': 59, 'max': 76}
11}
12
13hcl_matcher = re.compile('#[0-9a-f]{6}')
14
15
6@dataclass 16@dataclass
7class Passport: 17class Passport:
8 byr: int = None 18 byr: int = None
@@ -14,12 +24,17 @@ class Passport:
14 pid: str = None 24 pid: str = None
15 cid: str = None 25 cid: str = None
16 26
27 def __post_init__(self):
28 self.byr = self.byr and int(self.byr)
29 self.iyr = self.iyr and int(self.iyr)
30 self.eyr = self.eyr and int(self.eyr)
31
17 32
18class Assignment(BaseAssignment): 33class Assignment(BaseAssignment):
19 def create_passport(self, items: List[List]) -> Passport: 34 def create_passport(self, items: List[List]) -> Passport:
20 return Passport(**dict(items)) 35 return Passport(**dict(items))
21 36
22 def read_input(self, example = False) -> Generator: 37 def read_input(self, example=False) -> Generator:
23 passport = [] 38 passport = []
24 39
25 for item in super().read_input(example): 40 for item in super().read_input(example):
@@ -43,6 +58,7 @@ class Assignment(BaseAssignment):
43 valid_passports += 1 58 valid_passports += 1
44 return valid_passports 59 return valid_passports
45 60
61
46class AssignmentOne(Assignment): 62class AssignmentOne(Assignment):
47 def valid_passport(self, passport: Passport) -> bool: 63 def valid_passport(self, passport: Passport) -> bool:
48 return all([ 64 return all([
@@ -51,5 +67,41 @@ class AssignmentOne(Assignment):
51 if key != 'cid' 67 if key != 'cid'
52 ]) 68 ])
53 69
70
54class AssignmentTwo(Assignment): 71class AssignmentTwo(Assignment):
55 pass \ No newline at end of file 72 def valid_byr(self, byr: int):
73 return byr and 1920 <= byr <= 2002
74
75 def valid_iyr(self, iyr: int):
76 return iyr and 2010 <= iyr <= 2020
77
78 def valid_eyr(self, eyr: int):
79 return eyr and 2020 <= eyr <= 2030
80
81 def valid_hgt(self, hgt: str):
82 try:
83 match = hgt_matcher.match(hgt).groupdict()
84 unit = valid_units[match['unit']]
85 return unit['min'] <= int(match['value']) <= unit['max']
86 except (TypeError, AttributeError):
87 return False
88
89 def valid_hcl(self, hcl: str):
90 return hcl and hcl_matcher.match(hcl)
91
92 def valid_ecl(self, ecl: str):
93 return ecl and ecl in (
94 'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'
95 )
96
97 def valid_pid(self, pid: str):
98 return pid and len(pid) == 9
99
100 def valid_passport(self, passport: Passport):
101 return self.valid_byr(passport.byr) \
102 and self.valid_iyr(passport.iyr) \
103 and self.valid_eyr(passport.eyr) \
104 and self.valid_hgt(passport.hgt) \
105 and self.valid_hcl(passport.hcl) \
106 and self.valid_ecl(passport.ecl) \
107 and self.valid_pid(passport.pid)