-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol4.py
97 lines (77 loc) · 3.2 KB
/
sol4.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import utils
import re
def initialise():
# Passport Fields
# byr (Birth Year) four digits; at least 1920 and at most 2002.
# iyr (Issue Year) four digits; at least 2010 and at most 2020.
# eyr (Expiration Year) four digits; at least 2020 and at most 2030.
# hgt (Height) a number followed by either cm or in:
# If cm, the number must be at least 150 and at most 193.
# If in, the number must be at least 59 and at most 76.
# hcl (Hair Color) a # followed by exactly six characters 0-9 or a-f.
# ecl (Eye Color) exactly one of: amb blu brn gry grn hzl oth.
# pid (Passport ID) a nine-digit number, including leading zeroes.
# cid (Country ID) ignored, missing or not.
global batchInput
batchInput = utils.loadInputFile("input_4.txt")
def validatePassport(passport, check):
# check all required fields are present
if not all(fields in passport for fields in ("byr", "iyr", "eyr", "hgt",
"hcl", "ecl", "pid")):
return False
if check == "strict":
if (len(passport["byr"]) != 4 or int(passport["byr"]) < 1920
or int(passport["byr"]) > 2002):
return False
if (len(passport["iyr"]) != 4 or int(passport["iyr"]) < 2010
or int(passport["iyr"]) > 2020):
return False
if (len(passport["eyr"]) != 4 or int(passport["eyr"]) < 2020
or int(passport["eyr"]) > 2030):
return False
heightIn = re.match("^([0-9]{3})cm$", passport["hgt"]) # 150-193
heightCm = re.match("^([0-9]{2})in$", passport["hgt"]) # 59-76
if heightIn is None and heightCm is None:
return False
elif heightIn is not None:
if int(heightIn.group(1)) < 150 or int(heightIn.group(1)) > 193:
return False
elif heightCm is not None:
if int(heightCm.group(1)) < 59 or int(heightCm.group(1)) > 76:
return False
if not re.search("^#[a-f0-9]{6}$", passport["hcl"]):
return False
if passport["ecl"] not in ["amb", "blu", "brn", "gry",
"grn", "hzl", "oth"]:
return False
if not re.search("^[0-9]{9}$", passport["pid"]):
return False
return True
def processBatch(check):
global batchInput
passport = dict()
valid = 0
for line in batchInput:
# a blank line is the data separator
# when seeing this, validate and start new passport
if line == "":
if validatePassport(passport, check):
valid += 1
passport = dict()
else:
fields = line.split(" ")
for field in fields:
key = field.split(":")[0]
value = field.split(":")[1]
passport[key] = value
# validate final passport in the file
if validatePassport(passport, check):
valid += 1
return valid
def part1():
return processBatch("lazy")
def part2():
return processBatch("strict")
initialise()
print("Valid passports [lazy]:", part1()) # 260
print("Valid passports [strict]:", part2()) # 153