-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4_part1.py
executable file
·49 lines (37 loc) · 1.41 KB
/
day4_part1.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
#!/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
valid_passports.append(passport)
print(f"Valid passports: {len(valid_passports)}")