-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.py
49 lines (31 loc) · 1.17 KB
/
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
from lib import *
input = read_input(2020, 4)
out = 0
for kv in map(str.split, input.split("\n\n")):
x = {x.split(":")[0] for x in kv}
out += all(e in x for e in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"])
print(out)
def valid(x):
if not re.match(r"^\d{4}$", x.get("byr", "")) or not 1920 <= int(x["byr"]) <= 2002:
return False
if not re.match(r"^\d{4}$", x.get("iyr", "")) or not 2010 <= int(x["iyr"]) <= 2020:
return False
if not re.match(r"^\d{4}$", x.get("eyr", "")) or not 2020 <= int(x["eyr"]) <= 2030:
return False
if m := re.match(r"^(\d+)(in|cm)$", x.get("hgt", "")):
a, b = m.groups()
if b == "cm" and not 150 <= int(a) <= 193:
return False
elif b == "in" and not 59 <= int(a) <= 76:
return False
else:
return False
if not re.match(r"^#[\da-f]{6}$", x.get("hcl", "")):
return False
if x.get("ecl", "") not in "amb blu brn gry grn hzl oth".split():
return False
return bool(re.match(r"^\d{9}$", x.get("pid", "")))
out = 0
for kv in map(str.split, input.split("\n\n")):
out += valid(dict(x.split(":") for x in kv))
print(out)