-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-2.py
More file actions
50 lines (45 loc) · 1.68 KB
/
04-2.py
File metadata and controls
50 lines (45 loc) · 1.68 KB
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
import re
passport_list = []
with open("04.in", "r") as file:
current_passport = {}
for l in file:
if l == "\n":
passport_list.append(current_passport)
current_passport = {}
else:
item_list = l.strip().split()
for item in item_list:
current_passport[item.split(":")[0]] = item.split(":")[1]
passport_list.append(current_passport)
valid_count = 0
for pp in passport_list:
valid_sub_count = 0
for k in pp.keys():
if k == "byr":
if 1920 <= int(pp["byr"]) <= 2002:
valid_sub_count += 1
if k == "iyr":
if 2010 <= int(pp["iyr"]) <= 2020:
valid_sub_count += 1
if k == "eyr":
if 2020 <= int(pp["eyr"]) <= 2030:
valid_sub_count += 1
if k == "hgt":
if pp["hgt"][-2:] == "in":
if 59 <= int(pp["hgt"][:-2]) <= 76:
valid_sub_count += 1
elif pp["hgt"][-2:] == "cm":
if 150 <= int(pp["hgt"][:-2]) <= 193:
valid_sub_count += 1
if k == "hcl":
if re.fullmatch(r"#[a-f0-9]{6}", pp["hcl"]) != None:
valid_sub_count += 1
if k == "ecl":
if pp["ecl"] in set(["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]):
valid_sub_count += 1
if k == "pid":
if re.fullmatch(r"[0-9]{9}", pp["pid"]) != None:
valid_sub_count += 1
if valid_sub_count == 7:
valid_count += 1
print(valid_count)