Advertisement
bob_f

AOC2020D4.py

Sep 23rd, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import pprint
  2.  
  3. def get_puzzle_input(a_file_name: str) -> list[dict[str, str]]:
  4.     passport = dict()
  5.     passports: list[dict[str, str]] = []
  6.     passport_chunks: list[str] = []
  7.  
  8.     def get_passport(a_passport_chunks: list[str]) -> dict[str, str]:
  9.         passport_line = ' '.join(a_passport_chunks)
  10.         return {x[0] : x[1] for x in (t.split(':') for t in passport_line.split()) }
  11.  
  12.     with open(a_file_name) as INFILE:
  13.         passport_lines = [line.strip() for line in INFILE]
  14.  
  15.     for passport_line in passport_lines:
  16.         if passport_line == '':
  17.             passports.append(get_passport(passport_chunks))
  18.             passport_chunks = []
  19.         else:
  20.             passport_chunks.append(passport_line)
  21.  
  22.     if passport_chunks:
  23.         passports.append(get_passport(passport_chunks))
  24.  
  25.     return passports    
  26.  
  27. def is_valid_passport(a_passport: dict[str, str]) -> bool:
  28.     is_valid: bool = False
  29.  
  30.     if (len(a_passport) >= 7
  31.         and all(k in a_passport for k in ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'])):
  32.  
  33.             is_valid = True
  34.  
  35.     return is_valid
  36.  
  37. passports = get_puzzle_input('aoc_2020/aoc_2020_day_04.txt')
  38. # pprint.pprint(passports, width=132)
  39.  
  40. print(f"p1={sum([is_valid_passport(passport) for passport in passports])}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement