diff options
| author | 2021-12-02 17:39:03 +0100 | |
|---|---|---|
| committer | 2021-12-02 17:39:03 +0100 | |
| commit | 4dec21f362c03136e9811a4f4c162fcd8c50544e (patch) | |
| tree | cd90c52c7c936fdbe5fc7f22f3f5bf3240faf9a8 /day4 | |
| parent | 37aa8eec0498d7e8491084711132f16db9129a39 (diff) | |
| download | 2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.gz 2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.bz2 2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.zip | |
Added day 10
Diffstat (limited to 'day4')
| -rw-r--r-- | day4/__init__.py | 107 | ||||
| -rw-r--r-- | day4/example.txt | 13 | ||||
| -rw-r--r-- | day4/input.txt | 1023 |
3 files changed, 0 insertions, 1143 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) | ||
diff --git a/day4/example.txt b/day4/example.txt deleted file mode 100644 index 3b38741..0000000 --- a/day4/example.txt +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | ecl:gry pid:860033327 eyr:2020 hcl:#fffffd | ||
| 2 | byr:1937 iyr:2017 cid:147 hgt:183cm | ||
| 3 | |||
| 4 | iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 | ||
| 5 | hcl:#cfa07d byr:1929 | ||
| 6 | |||
| 7 | hcl:#ae17e1 iyr:2013 | ||
| 8 | eyr:2024 | ||
| 9 | ecl:brn pid:760753108 byr:1931 | ||
| 10 | hgt:179cm | ||
| 11 | |||
| 12 | hcl:#cfa07d eyr:2025 pid:166559648 | ||
| 13 | iyr:2011 ecl:brn hgt:59in \ No newline at end of file | ||
diff --git a/day4/input.txt b/day4/input.txt deleted file mode 100644 index ba08163..0000000 --- a/day4/input.txt +++ /dev/null | |||
| @@ -1,1023 +0,0 @@ | |||
| 1 | ecl:#eef340 eyr:2023 hcl:#c0946f pid:244684338 iyr:2020 cid:57 byr:1969 hgt:152cm | ||
| 2 | |||
| 3 | pid:303807545 cid:213 ecl:gry hcl:#fffffd | ||
| 4 | eyr:2038 byr:1951 | ||
| 5 | hgt:171cm iyr:2011 | ||
| 6 | |||
| 7 | hcl:#c0946f byr:1933 eyr:2025 pid:517067213 hgt:173cm | ||
| 8 | ecl:hzl | ||
| 9 | iyr:2018 | ||
| 10 | |||
| 11 | pid:5253256652 byr:2009 hgt:152cm iyr:1989 eyr:1968 hcl:64cb63 ecl:hzl | ||
| 12 | |||
| 13 | iyr:2013 | ||
| 14 | pid:862607211 eyr:2020 | ||
| 15 | hgt:174cm | ||
| 16 | byr:1990 | ||
| 17 | ecl:blu hcl:#888785 | ||
| 18 | |||
| 19 | ecl:grn byr:1955 | ||
| 20 | pid:608098408 eyr:2027 iyr:2020 hcl:#b6652a cid:264 | ||
| 21 | hgt:181cm | ||
| 22 | |||
| 23 | byr:1998 ecl:hzl eyr:2021 | ||
| 24 | hcl:#866857 hgt:189cm iyr:2030 | ||
| 25 | pid:013735467 | ||
| 26 | |||
| 27 | pid:075417314 hcl:#b6652a byr:1957 | ||
| 28 | iyr:2011 | ||
| 29 | hgt:154cm | ||
| 30 | eyr:2025 | ||
| 31 | ecl:amb | ||
| 32 | |||
| 33 | iyr:1993 hgt:74cm eyr:1960 | ||
| 34 | byr:2029 | ||
| 35 | hcl:293244 ecl:#3cb5e5 pid:4861232363 | ||
| 36 | |||
| 37 | hgt:153cm hcl:#6b5442 pid:065240897 byr:1932 | ||
| 38 | cid:196 ecl:blu eyr:2028 iyr:2019 | ||
| 39 | |||
| 40 | eyr:2024 ecl:amb | ||
| 41 | hcl:#866857 byr:1922 pid:496904942 hgt:164cm iyr:2020 | ||
| 42 | |||
| 43 | pid:847705161 byr:1951 cid:124 iyr:1991 | ||
| 44 | eyr:2028 ecl:dne | ||
| 45 | hcl:6b2d28 hgt:172cm | ||
| 46 | |||
| 47 | byr:2007 iyr:2029 ecl:hzl eyr:2026 hgt:168in | ||
| 48 | hcl:z | ||
| 49 | pid:63151612 | ||
| 50 | |||
| 51 | iyr:2027 hgt:172in | ||
| 52 | cid:174 byr:2014 hcl:z eyr:1921 | ||
| 53 | ecl:grt pid:#cebdc2 | ||
| 54 | |||
| 55 | ecl:amb hcl:#d09c0f | ||
| 56 | iyr:2015 hgt:156cm eyr:2026 byr:1962 | ||
| 57 | pid:086120872 | ||
| 58 | |||
| 59 | cid:125 | ||
| 60 | pid:837849996 | ||
| 61 | byr:1958 ecl:brn hgt:166cm hcl:#ceb3a1 eyr:2028 iyr:2012 | ||
| 62 | |||
| 63 | ecl:gry hgt:180cm | ||
| 64 | pid:939059935 eyr:2023 byr:1977 hcl:#b6652a | ||
| 65 | |||
| 66 | hgt:190in byr:2029 eyr:1984 | ||
| 67 | ecl:gry hcl:z pid:055092916 iyr:2010 | ||
| 68 | |||
| 69 | hgt:63in ecl:blu byr:1940 iyr:2012 eyr:2029 hcl:#7d3b0c | ||
| 70 | pid:163033394 | ||
| 71 | |||
| 72 | eyr:2040 pid:181cm | ||
| 73 | iyr:1928 hgt:76cm hcl:d8aafb byr:1949 cid:275 ecl:xry | ||
| 74 | |||
| 75 | hcl:#cfa07d ecl:oth byr:1948 eyr:2021 | ||
| 76 | pid:719690182 hgt:179cm iyr:2020 | ||
| 77 | |||
| 78 | byr:1939 cid:266 | ||
| 79 | eyr:2026 iyr:2017 hcl:#f116d2 | ||
| 80 | ecl:hzl pid:522421825 | ||
| 81 | hgt:68in | ||
| 82 | |||
| 83 | hcl:z hgt:61in | ||
| 84 | cid:115 pid:180cm iyr:2030 byr:2014 eyr:2032 ecl:hzl | ||
| 85 | |||
| 86 | ecl:xry | ||
| 87 | eyr:2028 pid:191cm iyr:2015 hgt:150cm byr:2029 | ||
| 88 | |||
| 89 | eyr:2027 | ||
| 90 | hcl:#a97842 pid:997937480 hgt:61in iyr:2019 | ||
| 91 | byr:1921 | ||
| 92 | ecl:grn | ||
| 93 | |||
| 94 | hgt:163cm | ||
| 95 | pid:912318455 iyr:2016 byr:1988 ecl:brn hcl:#a97842 cid:221 eyr:2029 | ||
| 96 | |||
| 97 | cid:333 hcl:#c0946f | ||
| 98 | hgt:163cm iyr:2018 eyr:2027 ecl:oth | ||
| 99 | |||
| 100 | cid:70 hcl:#c0946f | ||
| 101 | pid:961507902 byr:1949 ecl:grn iyr:2017 eyr:2029 hgt:69in | ||
| 102 | |||
| 103 | hgt:169cm ecl:amb | ||
| 104 | iyr:2013 pid:496169901 | ||
| 105 | byr:1943 eyr:2028 hcl:#ceb3a1 | ||
| 106 | cid:249 | ||
| 107 | |||
| 108 | ecl:blu cid:343 | ||
| 109 | iyr:1998 hgt:154in hcl:#efcc98 byr:1972 | ||
| 110 | |||
| 111 | pid:508213120 iyr:2012 eyr:2025 hcl:z hgt:70cm byr:1976 | ||
| 112 | ecl:brn | ||
| 113 | |||
| 114 | iyr:1924 | ||
| 115 | eyr:2005 | ||
| 116 | hcl:z | ||
| 117 | hgt:167 pid:154cm | ||
| 118 | byr:2007 | ||
| 119 | |||
| 120 | pid:761333244 hgt:180cm iyr:2017 hcl:#18171d eyr:2021 | ||
| 121 | |||
