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_passport(a_passport: dict[str, str]) -> 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'])):
- is_valid = True
- return is_valid
- passports = get_puzzle_input('aoc_2020/aoc_2020_day_04.txt')
- # pprint.pprint(passports, width=132)
- print(f"p1={sum([is_valid_passport(passport) for passport in passports])}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement