-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4_part2.py
executable file
·71 lines (59 loc) · 2.38 KB
/
day4_part2.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
#!/usr/bin/python3
import loader
import logging
import re
#logging.basicConfig(level=logging.DEBUG)
valid_passport_required_keys = ["byr",
"iyr",
"eyr",
"hgt",
"hcl",
"ecl",
"pid"]
#"cid"]
passport_string_list = loader.string_list("input_day4")
all_passports = []
current_passport = {}
for line in passport_string_list:
logging.debug(f"Got line: {line}")
if line == "":
logging.debug(f"Found blank line. Added {current_passport} to all_passports")
all_passports.append(current_passport)
current_passport = {}
continue
else:
field_data = line.split()
for field in field_data:
split_field = field.split(":")
current_passport[split_field[0]] = split_field[1]
if len(current_passport.keys()) != 0:
all_passports.append(current_passport) # file does not end with blank line
valid_passports = []
for passport in all_passports:
if len(set(valid_passport_required_keys) - set(passport.keys())) != 0:
logging.debug(f"Rejecting passport {passport} due to invalid field set.")
continue
if int(passport["byr"]) < 1920 or int(passport["byr"]) > 2002:
logging.debug(f"Rejecting passport {passport} due to invalid byr.")
continue
if int(passport["iyr"]) < 2010 or int(passport["iyr"]) > 2020:
logging.debug(f"Rejecting passport {passport} due to invalid iyr.")
continue
if int(passport["eyr"]) < 2020 or int(passport["eyr"]) > 2030:
continue
height_unit = passport["hgt"][-2:]
height_number = int(passport["hgt"][:-2])
if height_unit == "cm" and (height_number < 150 or height_number > 193):
continue
elif height_unit == "in" and (height_number < 59 or height_number > 76):
continue
elif height_unit not in ("in", "cm"):
continue #invalid height unit
if not re.match('^#[0-9a-f]{6,6}$',passport["hcl"]):
continue
if passport["ecl"] not in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth"):
continue
if not re.match('^[0-9]{9,9}$',passport["pid"]):
continue
valid_passports.append(passport)
print(f"Valid passports: {len(valid_passports)}")