summaryrefslogtreecommitdiffstats
path: root/day4/__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 /day4/__init__.py
parent37aa8eec0498d7e8491084711132f16db9129a39 (diff)
download2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.gz
2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.tar.bz2
2021-4dec21f362c03136e9811a4f4c162fcd8c50544e.zip
Added day 10
Diffstat (limited to 'day4/__init__.py')
-rw-r--r--day4/__init__.py107
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 @@
1import re
2from dataclasses import dataclass
3from typing import Generator, List
4
5from aoc import BaseAssignment
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
16@dataclass
17class 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
33class 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
62class 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
71class 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)