-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_04.py
64 lines (51 loc) · 1.69 KB
/
day_04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import aoc_helper
HEIGHT = re.compile(r"(\d+)(cm|in)")
COLOUR = re.compile(r"#[0-9a-f]{6}")
PID = re.compile(r"\d{9}")
raw = aoc_helper.fetch(4, year=2020)
# print(raw)
FIELDS = {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}
def parse_raw():
passports = raw.split("\n\n")
fields = [passport.split() for passport in passports]
return [
{field.split(":")[0]: field.split(":")[1] for field in fields_}
for fields_ in fields
]
data = parse_raw()
def part_one():
valid = 0
for passport in data:
if set(passport.keys()) & FIELDS == FIELDS:
valid += 1
return valid
def part_two():
valid = 0
for passport in data:
if set(passport.keys()) & FIELDS == FIELDS:
if not (1920 <= int(passport["byr"]) <= 2002):
continue
if not (2010 <= int(passport["iyr"]) <= 2020):
continue
if not (2020 <= int(passport["eyr"]) <= 2030):
continue
if not (
(match := HEIGHT.fullmatch(passport["hgt"]))
and (
(150 <= int(match[1]) <= 193)
if match[2] == "cm"
else (59 <= int(match[1]) <= 76)
)
):
continue
if not COLOUR.fullmatch(passport["hcl"]):
continue
if not passport["ecl"] in "amb blu brn gry grn hzl oth".split():
continue
if not PID.fullmatch(passport["pid"]):
continue
valid += 1
return valid
aoc_helper.lazy_submit(day=4, year=2020, solution=part_one)
aoc_helper.lazy_submit(day=4, year=2020, solution=part_two)