Added 2020 (python)
This commit is contained in:
BIN
2020/4/__pycache__/prog.cpython-38.pyc
Normal file
BIN
2020/4/__pycache__/prog.cpython-38.pyc
Normal file
Binary file not shown.
1138
2020/4/input
Normal file
1138
2020/4/input
Normal file
File diff suppressed because it is too large
Load Diff
81
2020/4/prog.py
Normal file
81
2020/4/prog.py
Normal file
@ -0,0 +1,81 @@
|
||||
import re
|
||||
|
||||
def extract_passports(passports: list):
|
||||
ret = []
|
||||
kv_re = re.compile(r'(\w+):(#?\w+)')
|
||||
|
||||
for passport in passports:
|
||||
dic = {}
|
||||
for key, val in kv_re.findall(passport):
|
||||
dic[key] = val
|
||||
ret.append(dic)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def get_input(sample = False):
|
||||
with open("sample" if sample else "input", "r") as f:
|
||||
content = f.read()
|
||||
passports = content.split("\n\n")
|
||||
return extract_passports(passports)
|
||||
|
||||
def is_passport_valid_1(passport: dict):
|
||||
fields = {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}
|
||||
keys = set(passport.keys())
|
||||
return keys == fields or keys == fields | {"cid"}
|
||||
|
||||
between = lambda m1, x, m2: int(m1) <= int(x) and int(x) <= int(m2);
|
||||
|
||||
def is_height_valid(height: str):
|
||||
h_re = re.compile(r'^(\d+)(cm|in)$')
|
||||
if h_re.match(height) == None: return False
|
||||
|
||||
number, measure = h_re.findall(height)[0]
|
||||
|
||||
return (measure == "cm" and between(150, number, 193)) or (measure == "in" and between(59, number, 76))
|
||||
|
||||
|
||||
def is_passport_valid_2(passport: dict):
|
||||
is_four_digits = lambda x: re.match(r'^\d{4}$', x) != None
|
||||
|
||||
rules = {"byr": lambda x: is_four_digits(x) and between(1920, x, 2002),
|
||||
"iyr": lambda x: is_four_digits(x) and between(2010, x, 2020),
|
||||
"eyr": lambda x: is_four_digits(x) and between(2020, x, 2030),
|
||||
"hgt": is_height_valid,
|
||||
"hcl": lambda x: re.match(r'^#[a-f0-9]{6}$', x) != None,
|
||||
"ecl": lambda x: x in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"],
|
||||
"pid": lambda x: re.match(r'^\d{9}$', x) != None,
|
||||
"cid": lambda x: True}
|
||||
|
||||
# try:
|
||||
# print(passport["hcl"], rules["hcl"](passport["hcl"]))
|
||||
# except KeyError as e:
|
||||
# pass
|
||||
|
||||
for key, val in passport.items():
|
||||
if not rules[key](val):
|
||||
return False
|
||||
return True
|
||||
|
||||
def get_result(passports: list, part = 1):
|
||||
ret = 0
|
||||
print(f"{len(passports)=}")
|
||||
for p in passports:
|
||||
if (part == 1 and is_passport_valid_1(p)) or (part == 2 and is_passport_valid_2(p) and is_passport_valid_1(p)):
|
||||
ret += 1
|
||||
return ret
|
||||
|
||||
|
||||
def main(sample = False, part = 1):
|
||||
inp = get_input(sample = sample)
|
||||
res = get_result(inp, part = part)
|
||||
|
||||
if sample:
|
||||
expected = 2 if part == 2 else 0
|
||||
print(f"{res=}")
|
||||
print(res == expected)
|
||||
else:
|
||||
print(res)
|
||||
|
||||
|
||||
main(sample = False, part = 2)
|
13
2020/4/sample
Normal file
13
2020/4/sample
Normal file
@ -0,0 +1,13 @@
|
||||
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
||||
byr:1937 iyr:2017 cid:147 hgt:183cm
|
||||
|
||||
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
|
||||
hcl:#cfa07d byr:1929
|
||||
|
||||
hcl:#ae17e1 iyr:2013
|
||||
eyr:2024
|
||||
ecl:brn pid:760753108 byr:1931
|
||||
hgt:179cm
|
||||
|
||||
hcl:#cfa07d eyr:2025 pid:166559648
|
||||
iyr:2011 ecl:brn hgt:59in
|
50
2020/4/test.py
Normal file
50
2020/4/test.py
Normal file
@ -0,0 +1,50 @@
|
||||
from prog import get_input
|
||||
|
||||
expected = [
|
||||
{
|
||||
'ecl' : 'gry',
|
||||
'pid' : '860033327',
|
||||
'eyr' : '2020',
|
||||
'hcl' : '#fffffd',
|
||||
'byr' : '1937',
|
||||
'iyr' : '2017',
|
||||
'cid' : '147',
|
||||
'hgt' : '183cm',
|
||||
},
|
||||
|
||||
{
|
||||
'iyr' : '2013',
|
||||
'ecl' : 'amb',
|
||||
'cid' : '350',
|
||||
'eyr' : '2023',
|
||||
'pid' : '028048884',
|
||||
'hcl' : '#cfa07d',
|
||||
'byr' : '1929',
|
||||
},
|
||||
|
||||
{
|
||||
'hcl' : '#ae17e1',
|
||||
'iyr' : '2013',
|
||||
'eyr' : '2024',
|
||||
'ecl' : 'brn',
|
||||
'pid' : '760753108',
|
||||
'byr' : '1931',
|
||||
'hgt' : '179cm',
|
||||
},
|
||||
|
||||
{
|
||||
'hcl' : '#cfa07d',
|
||||
'eyr' : '2025',
|
||||
'pid' : '166559648',
|
||||
'iyr' : '2011',
|
||||
'ecl' : 'brn',
|
||||
'hgt' : '59in',
|
||||
},
|
||||
]
|
||||
|
||||
out = get_input(sample = True)
|
||||
for o, e in zip(out, expected):
|
||||
print(o)
|
||||
print(e)
|
||||
|
||||
print(out == expected)
|
Reference in New Issue
Block a user