Solve 4.2
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from itertools import combinations
|
||||
|
||||
with open('01-input') as f:
|
||||
with open('input') as f:
|
||||
numbers = {int(row.strip()) for row in f}
|
||||
|
||||
for n in numbers:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import re
|
||||
|
||||
with open('02-input') as f:
|
||||
with open('input') as f:
|
||||
passwords = [row.strip() for row in f]
|
||||
|
||||
pattern = re.compile(r'(\d*)-(\d*) (\w): (.*)')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import re
|
||||
|
||||
with open('03-input') as f:
|
||||
with open('input') as f:
|
||||
landscape = [row.strip() for row in f]
|
||||
|
||||
count = 0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import re
|
||||
|
||||
with open('04-input') as f:
|
||||
with open('input') as f:
|
||||
text = f.read()
|
||||
|
||||
while '\n\n\n' in text:
|
||||
@@ -12,12 +12,56 @@ print(sum(1 for p in passports if valid.issubset(p.keys())))
|
||||
|
||||
from functools import partial
|
||||
def int_val(least, most, n):
|
||||
return least<=n<=most
|
||||
return least<=int(n)<=most
|
||||
v_byr = partial(int_val, 1920, 2002)
|
||||
v_iyr = partial(int_val, 2010, 2020)
|
||||
v_eyr = partial(int_val, 2020, 2030)
|
||||
v_cm = partial(int_val, 150, 193)
|
||||
v_in = partial(int_val, 59, 76)
|
||||
|
||||
class NotValid(Exception):
|
||||
pass
|
||||
|
||||
def has_correct_keys(passport):
|
||||
if not valid.issubset(passport.keys()):
|
||||
raise NotValid("keys")
|
||||
def valid_field(key, func, passport):
|
||||
if not func(passport[key]):
|
||||
raise NotValid(key)
|
||||
def valid_ecl(passport):
|
||||
if passport["ecl"] not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth",]:
|
||||
raise NotValid("ecl")
|
||||
def valid_pid(passport):
|
||||
if not re.match(r"^\d{9}$", passport["pid"]):
|
||||
raise NotValid("pid")
|
||||
def valid_hcl(passport):
|
||||
if not re.match(r"^\#[0-9a-f]{6}$", passport["hcl"]):
|
||||
raise NotValid("hcl")
|
||||
def valid_hgt(passport):
|
||||
if not (m := re.match(r"^(\d+)(cm|in)$", passport["hgt"])):
|
||||
raise NotValid
|
||||
if m.group(2)=="in":
|
||||
if not v_in(m.group(1)):
|
||||
raise NotValid
|
||||
else:
|
||||
if not v_cm(m.group(1)):
|
||||
raise NotValid
|
||||
|
||||
checks = [has_correct_keys,
|
||||
partial(valid_field, "byr", v_byr),
|
||||
partial(valid_field, "iyr", v_iyr),
|
||||
partial(valid_field, "eyr", v_eyr),
|
||||
valid_ecl,
|
||||
valid_pid,
|
||||
valid_hcl,
|
||||
valid_hgt,
|
||||
]
|
||||
def is_valid(passport):
|
||||
valid.issubset(p.keys())
|
||||
try:
|
||||
for check in checks:
|
||||
check(passport)
|
||||
return True
|
||||
except NotValid as e:
|
||||
return False
|
||||
|
||||
print(len(list(filter(is_valid, passports))))
|
||||
|
||||
Reference in New Issue
Block a user