diff options
Diffstat (limited to 'day4/__init__.py')
| -rw-r--r-- | day4/__init__.py | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/day4/__init__.py b/day4/__init__.py deleted file mode 100644 index 89f697c..0000000 --- a/day4/__init__.py +++ /dev/null | |||
| @@ -1,107 +0,0 @@ | |||
| 1 | import re | ||
| 2 | from dataclasses import dataclass | ||
| 3 | from typing import Generator, List | ||
| 4 | |||
| 5 | from aoc import BaseAssignment | ||
| 6 | |||
| 7 | hgt_matcher = re.compile('(?P<value>\d+)(?P<unit>(in|cm))') | ||
| 8 | valid_units = { | ||
| 9 | 'cm': {'min': 150, 'max': 193}, | ||
| 10 | 'in': {'min': 59, 'max': 76} | ||
| 11 | } | ||
| 12 | |||
| 13 | hcl_matcher = re.compile('#[0-9a-f]{6}') | ||
| 14 | |||
| 15 | |||
| 16 | @dataclass | ||
| 17 | class Passport: | ||
| 18 | byr: int = None | ||
| 19 | iyr: int = None | ||
| 20 | eyr: int = None | ||
| 21 | hgt: str = None | ||
| 22 | hcl: str = None | ||
| 23 | ecl: str = None | ||
| 24 | pid: str = None | ||
| 25 | cid: str = None | ||
| 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 | |||
| 32 | |||
| 33 | class Assignment(BaseAssignment): | ||
| 34 | def create_passport(self, items: List[List]) -> Passport: | ||
| 35 | return Passport(**dict(items)) | ||
| 36 | |||
| 37 | def read_input(self, example=False) -> Generator: | ||
| 38 | passport = [] | ||
| 39 | |||
| 40 | for item in super().read_input(example): | ||
| 41 | if item == '': | ||
| 42 | yield self.create_passport(passport) | ||
| 43 | passport = [] | ||
| 44 | continue | ||
| 45 | |||
| 46 | for field in item.split(' '): | ||
| 47 | passport.append(field.split(':')) | ||
| 48 | |||
| 49 | yield self.create_passport(passport) | ||
| 50 | |||
| 51 | def valid_passport(self, passport: Passport): | ||
| 52 | raise NotImplementedError('Please implement valid_passport') | ||
| 53 | |||
| 54 | def run(self, input: Generator): | ||
| 55 | valid_passports = 0 | ||
| 56 | for passport in input: | ||
| 57 | if self.valid_passport(passport): | ||
| 58 | valid_passports += 1 | ||
| 59 | return valid_passports | ||
| 60 | |||
| 61 | |||
| 62 | class AssignmentOne(Assignment): | ||
| 63 | def valid_passport(self, passport: Passport) -> bool: | ||
| 64 | return all([ | ||
| 65 | val | ||
| 66 | for key, val in vars(passport).items() | ||
| 67 | if key != 'cid' | ||
| 68 | ]) | ||
| 69 | |||
| 70 | |||
| 71 | class AssignmentTwo(Assignment): | ||
| 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) | ||
