-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.py
37 lines (34 loc) · 1.14 KB
/
4.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
# Problem: https://adventofcode.com/2020/day/4
# Input
from sys import stdin
passports = [
dict([
field.split(":")
for field in passport.replace("\n", " ").split()
])
for passport in "".join(stdin).split("\n\n")
]
# Part 1
expectedfields = {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}
print(sum((
1 for passport in passports
if expectedfields <= set(passport.keys())
)))
# Part 2
import re
print(sum((
1 for passport in passports
if (
expectedfields <= set(passport.keys())
and len(passport["byr"]) == 4 and 1920 <= int(passport["byr"]) <= 2002
and len(passport["iyr"]) == 4 and 2010 <= int(passport["iyr"]) <= 2020
and len(passport["eyr"]) == 4 and 2020 <= int(passport["eyr"]) <= 2030
and (
(passport["hgt"][-2:] == "cm" and 150 <= int(passport["hgt"][:-2]) <= 193)
or (passport["hgt"][-2:] == "in" and 59 <= int(passport["hgt"][:-2]) <= 76)
)
and re.fullmatch(r"#[0-9a-f]{6}", passport["hcl"])
and passport["ecl"] in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth")
and re.fullmatch(r"\d{9}", passport["pid"])
)
)))