summaryrefslogtreecommitdiff
path: root/4_2020.py
blob: c77a5625dcc16e63caef55ba290b4c9bb7d1f22f (plain)
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
import fileinput
import re

p1 = 0
p2 = 0
passport = {}  # current passport


def in_range(s, lo, hi):
    return lo <= int(s) <= hi


lines = list(fileinput.input())
lines.append("")
for line in lines:
    line = line.strip()
    if not line:
        valid1 = all(
            [f in passport for f in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]]
        )
        if valid1:
            p1 += 1
            valid2 = True

            if not in_range(passport["byr"], 1920, 2002):
                valid2 = False
            if not in_range(passport["iyr"], 2010, 2020):
                valid2 = False
            if not in_range(passport["eyr"], 2020, 2030):
                valid2 = False

            ht = passport["hgt"]
            if ht.endswith("in"):
                if not in_range(ht[:-2], 59, 76):
                    valid2 = False
            elif ht.endswith("cm"):
                if not in_range(ht[:-2], 150, 193):
                    valid2 = False
            else:
                valid2 = False

            hcl = passport["hcl"]
            if hcl[0] != "#" or any([c not in "0123456789abcdef" for c in hcl[1:]]):
                valid2 = False

            ecl = passport["ecl"]
            if ecl not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
                valid2 = False

            pid = passport["pid"]
            if len(pid) != 9 or any([c not in "0123456789" for c in pid]):
                valid2 = False

            if valid2:
                p2 += 1
        passport = {}
    else:
        words = line.split()
        for word in words:
            k, v = word.split(":")
            passport[k] = v
print(p1)
print(p2)