Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pprint
- def get_puzzle_input(a_file_name: str) -> list[dict[str, str]]:
- passport = dict()
- passports: list[dict[str, str]] = []
- passport_chunks: list[str] = []
- def get_passport(a_passport_chunks: list[str]) -> dict[str, str]:
- passport_line = ' '.join(a_passport_chunks)
- return {x[0] : x[1] for x in (t.split(':') for t in passport_line.split()) }
- with open(a_file_name) as INFILE:
- passport_lines = [line.strip() for line in INFILE]
- for passport_line in passport_lines:
- if passport_line == '':
- passports.append(get_passport(passport_chunks))
- passport_chunks = []
- else:
- passport_chunks.append(passport_line)
- if passport_chunks:
- passports.append(get_passport(passport_chunks))
- return passports
- def is_valid_field(a_passport_field_name: str, a_passport_field_value: str) -> bool:
- is_valid: bool = False
- match a_passport_field_name:
- case 'byr':
- if len(a_passport_field_value) != 4:
- return False
- try:
- byr = int(a_passport_field_value)
- return True if 1920 <= byr <= 2002 else False
- except ValueError:
- return False
- case 'iyr':
- if len(a_passport_field_value) != 4:
- return False
- try:
- byr = int(a_passport_field_value)
- return True if 2010 <= byr <= 2020 else False
- except ValueError:
- return False
- case 'eyr':
- if len(a_passport_field_value) != 4:
- return False
- try:
- byr = int(a_passport_field_value)
- return True if 2020 <= byr <= 2030 else False
- except ValueError:
- return False
- case 'hgt':
- try:
- hgt = int(a_passport_field_value[:-2])
- except ValueError:
- return False
- if a_passport_field_value.endswith('cm'):
- if 150 <= hgt <= 193:
- return True
- if a_passport_field_value.endswith('in'):
- if 59 <= hgt <= 76:
- return True
- case 'hcl':
- hcl_set = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }
- if len(a_passport_field_value) != 7:
- return False
- if (a_passport_field_value[0] == '#'
- and all(hcl_suffix_char in hcl_set for hcl_suffix_char in a_passport_field_value[1:])):
- return True
- case 'ecl':
- if a_passport_field_value in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
- return True
- case 'pid':
- if len(a_passport_field_value) == 9:
- try:
- _ = int(a_passport_field_value)
- return True
- except ValueError:
- return False
- case 'cid':
- return True
- return is_valid
- def is_valid_passport(a_passport: dict[str, str], a_p1_check: bool = True) -> bool:
- is_valid: bool = False
- if (len(a_passport) >= 7
- and all(k in a_passport for k in ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'])):
- if a_p1_check:
- is_valid = True
- else:
- if (is_valid_field('byr', a_passport['byr'])
- and is_valid_field('iyr', a_passport['iyr'])
- and is_valid_field('eyr', a_passport['eyr'])
- and is_valid_field('hgt', a_passport['hgt'])
- and is_valid_field('hcl', a_passport['hcl'])
- and is_valid_field('ecl', a_passport['ecl'])
- and is_valid_field('pid', a_passport['pid'])):
- is_valid = True
- return is_valid
- passports = get_puzzle_input('aoc_2020/aoc_2020_day_04.txt')
- print(f"p1={sum([is_valid_passport(passport) for passport in passports])}")
- print(f"p2={sum([is_valid_passport(passport, a_p1_check=False) for passport in passports])}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement