-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-passports.py
executable file
·82 lines (62 loc) · 1.54 KB
/
valid-passports.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/python3
from sys import stdin
import re
all_ok = 0
ok_without_cid = 0
def validate_byr(byr):
byr = int(byr)
return (byr >= 1920) and (byr <= 2002)
def validate_iyr(iyr):
iyr = int(iyr)
return (iyr >= 2010) and (iyr <= 2020)
def validate_eyr(eyr):
eyr = int(eyr)
return (eyr >= 2020) and (eyr <= 2030)
def validate_hgt(hgt):
regex = "^(1([5-8][0-9]|9[0-3])cm|(59|6[0-9]|7[0-6])in)$"
return re.match(regex, hgt) != None
def validate_hcl(hcl):
regex = "^#[0-9a-f]{6}$"
return re.match(regex, hcl) != None
def validate_ecl(ecl):
valid_colours = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
return ecl in valid_colours
def validate_pid(pid):
regex = "^[0-9]{9}$"
return re.match(regex, pid) != None
def validate(passport):
global all_ok
global ok_without_cid
required_fields = {
"byr": validate_byr,
"iyr": validate_iyr,
"eyr": validate_eyr,
"hgt": validate_hgt,
"hcl": validate_hcl,
"ecl": validate_ecl,
"pid": validate_pid,
}
for field, validator in required_fields.items():
if not field in passport:
return
if validator(passport[field]) != True:
return
all_ok += 1
if "cid" in passport:
ok_without_cid += 1
def main():
current = {}
for line in stdin:
line = line.strip()
if line == "":
validate(current)
current = {}
continue
for field in line.split(" "):
fieldname, value = field.split(":")
current[fieldname] = value
if len(current) > 0:
validate(current)
print("valid: {}; of those, without cid: {}".format(all_ok, ok_without_cid))
if __name__ == "__main__":
main()