Some cleanup, added a few comments all around

This commit is contained in:
Teo-CD 2020-12-05 00:04:09 +01:00
parent ad04dffb69
commit f44d5b9e66
6 changed files with 51 additions and 17 deletions

View file

@ -1,14 +1,16 @@
import re
input_file = "/tmp/aocinput"
input_file = "input.txt"
height_re = re.compile(r'^([0-9]+)(in|cm)$')
passport_id_re = re.compile(r'^[0-9]{9}$')
color_re = re.compile(r'^#[0-9a-f]{6}$')
def check_limits(to_test, min_valid, max_valid):
return False if (to_test < min_valid or to_test > max_valid) else True
def validate_passport(record):
for field in record:
if field[0] == "byr":
@ -24,16 +26,16 @@ def validate_passport(record):
regex_match = height_re.match(field[1])
if not regex_match:
return False
if regex_match.groups()[1] == "cm" and not check_limits(int(regex_match.groups()[0]), 150,193):
if regex_match.groups()[1] == "cm" and not check_limits(int(regex_match.groups()[0]), 150, 193):
return False
elif regex_match.groups()[1] == "in" and not check_limits(int(regex_match.groups()[0]),59,76):
elif regex_match.groups()[1] == "in" and not check_limits(int(regex_match.groups()[0]), 59, 76):
return False
elif field[0] == "hcl":
regex_match = color_re.match(field[1])
if not regex_match:
return False
elif field[0] == "ecl":
if field[1] not in ["amb","blu","brn","gry","grn","hzl","oth"]:
if field[1] not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
return False
elif field[0] == "pid":
regex_match = passport_id_re.match(field[1])
@ -57,7 +59,9 @@ with open(input_file) as passports:
line = passports.readline()
continue
# rstrip() strips the string from end of line characters et alia.
for raw_entry in line.rstrip().split(" "):
# Split the different fields, then split them in (key,value) tuples
records[current_index].append(tuple(raw_entry.split(":")))
line = passports.readline()
@ -75,4 +79,4 @@ for record in records:
if is_north_pole_credential and validate_passport(record):
valid_passports += 1
print(f"There are {valid_passports} valid passports out of {len(records)}")
print(f"There are {valid_passports} valid passports out of {len(records)}")