-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
46 lines (40 loc) · 1.39 KB
/
day04.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
from aocd.models import Puzzle
import re
input: str = Puzzle(day=4, year=2020).input_data
fields = "byr iyr eyr hgt hcl ecl pid cid"
fields = set(fields.split(" "))
def part1():
passports = input.split("\n\n")
passports = [" ".join(t.split("\n")) for t in passports]
passports = [t.split(" ") for t in passports]
passports = [[w.split(":")[0] for w in t] for t in passports]
validPassports = [t for t in passports if set(t + ["cid"]) == fields]
return len(validPassports)
def part2():
rules = [
("byr", lambda x: 1920 <= int(x) <= 2002),
("iyr", lambda x: 2010 <= int(x) <= 2020),
("eyr", lambda x: 2020 <= int(x) <= 2030),
(
"hgt",
lambda x: len(x) > 2
and (
150 <= int(x[:-2]) <= 193 if x[-2:] == "cm" else 59 <= int(x[:-2]) <= 76
),
),
(
"hcl",
lambda x: len(x) == 7
and x[0] == "#"
and all(c in "0123456789abcdef" for c in x[1:]),
),
("ecl", lambda x: x in "amb blu brn gry grn hzl oth"),
("pid", lambda x: len(x) == 9 and all("0" <= c <= "9" for c in x)),
]
return sum(
all(key in passport and f(passport[key]) for key, f in rules)
for passport in (
dict(map(lambda x: x.split(":"), part))
for part in map(str.split, input.split("\n\n"))
)
)