Skip to content

Commit 3726327

Browse files
committed
some nicer formatting and cleanup
1 parent eddd28d commit 3726327

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

day04/day04.py

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,55 @@
33
import re
44
from typing import Dict
55

6+
67
def check_passport(passport: Dict[str, str], deep_check: bool = False) -> bool:
7-
required_keys = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
8-
optional_keys = ['cid']
9-
valid = len(set(required_keys).intersection(set(passport.keys()))) == len(required_keys)
8+
required_keys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
9+
optional_keys = ["cid"]
10+
valid = len(set(required_keys).intersection(set(passport.keys()))) == len(
11+
required_keys
12+
)
1013

1114
if deep_check and valid:
1215
checks = []
13-
"""check the following fields
14-
15-
16-
byr (Birth Year) - four digits; at least 1920 and at most 2002.
17-
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
18-
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
19-
hgt (Height) - a number followed by either cm or in:
20-
If cm, the number must be at least 150 and at most 193.
21-
If in, the number must be at least 59 and at most 76.
22-
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
23-
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
24-
pid (Passport ID) - a nine-digit number, including leading zeroes.
25-
cid (Country ID) - ignored, missing or not.
16+
"""check the following fields:
2617
27-
"""
28-
checks.append(1920 <= int(passport['byr']) <= 2002)
29-
checks.append(2010 <= int(passport['iyr']) <= 2020)
30-
checks.append(2020 <= int(passport['eyr']) <= 2030)
31-
checks.append(passport['hgt'][-2:] in ['cm', 'in'] and (passport['hgt'][-2:] in "in" and int(passport['hgt'][:-2]) >= 59 and int(passport['hgt'][:-2])<=76) or (passport['hgt'][-2:] in "cm" and int(passport['hgt'][:-2]) >= 150 and int(passport['hgt'][:-2])<=193))
32-
checks.append(bool(re.match("#[0-9a-f]{6}", passport['hcl'])))
33-
checks.append(passport['ecl'] in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'])
34-
checks.append(len(passport['pid']) == 9)
18+
byr (Birth Year) - four digits; at least 1920 and at most 2002.
19+
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
20+
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
21+
hgt (Height) - a number followed by either cm or in:
22+
If cm, the number must be at least 150 and at most 193.
23+
If in, the number must be at least 59 and at most 76.
24+
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
25+
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
26+
pid (Passport ID) - a nine-digit number, including leading zeroes.
27+
cid (Country ID) - ignored, missing or not.
28+
"""
29+
checks.append(1920 <= int(passport["byr"]) <= 2002)
30+
checks.append(2010 <= int(passport["iyr"]) <= 2020)
31+
checks.append(2020 <= int(passport["eyr"]) <= 2030)
32+
checks.append(
33+
passport["hgt"][-2:] in ["cm", "in"]
34+
and (
35+
passport["hgt"][-2:] in "in"
36+
and int(passport["hgt"][:-2]) >= 59
37+
and int(passport["hgt"][:-2]) <= 76
38+
)
39+
or (
40+
passport["hgt"][-2:] in "cm"
41+
and int(passport["hgt"][:-2]) >= 150
42+
and int(passport["hgt"][:-2]) <= 193
43+
)
44+
)
45+
checks.append(bool(re.match("#[0-9a-f]{6}", passport["hcl"])))
46+
checks.append(
47+
passport["ecl"] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
48+
)
49+
checks.append(len(passport["pid"]) == 9)
3550
return all(checks)
3651
else:
3752
return valid
53+
54+
3855
if __name__ == "__main__":
3956
with open(sys.argv[1], "r") as infile:
4057
passports = []
@@ -49,5 +66,13 @@ def check_passport(passport: Dict[str, str], deep_check: bool = False) -> bool:
4966
for value in line:
5067
k, v = value.strip().split(":")
5168
passport[k] = v
52-
print("part 1: ", sum([check_passport(passport) for passport in passports]), " valid passports")
53-
print("part 2: ", sum([check_passport(passport, True) for passport in passports]), " valid passports")
69+
print(
70+
"part 1: ",
71+
sum([check_passport(passport) for passport in passports]),
72+
" valid passports",
73+
)
74+
print(
75+
"part 2: ",
76+
sum([check_passport(passport, True) for passport in passports]),
77+
" valid passports",
78+
)

0 commit comments

Comments
 (0)