Advertisement
bob_f

AOC2020D05.py

Sep 24th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. def get_puzzle_data(a_file_name: str) -> list[str]:
  2.     with open(a_file_name) as INFILE:
  3.         return [line.rstrip() for line in INFILE]
  4.  
  5. def binary_split(a_front_or_back: str, a_lo: int, a_hi: int) -> tuple[int, int]:
  6.     new_lo: int
  7.     new_hi: int
  8.     range_length = a_hi - a_lo + 1
  9.  
  10.     if a_front_or_back in 'fFlL':
  11.         new_lo = a_lo
  12.         new_hi = a_lo + (range_length // 2) - 1
  13.     elif a_front_or_back in 'bBrR':
  14.         new_lo = a_lo + (range_length // 2)
  15.         new_hi = a_hi
  16.  
  17.     return new_lo, new_hi
  18.  
  19. def get_seating_id(a_boarding_pass: str) -> int:
  20.     seat_id: int = 0
  21.  
  22.     lo, hi = 0, 127
  23.  
  24.     for split_type in a_boarding_pass[0:7]:
  25.         lo, hi = binary_split(split_type, lo, hi)
  26.  
  27.     assert lo == hi, 'Er...'
  28.     seat_row = lo
  29.     # print(f"Row={lo}")
  30.  
  31.     lo, hi = 0, 7
  32.  
  33.     for split_type in a_boarding_pass[7:]:
  34.         lo, hi = binary_split(split_type, lo, hi)
  35.  
  36.     assert lo == hi, 'Er...'
  37.     seat_col = lo
  38.     # print(f"Column={lo}")
  39.  
  40.     return (8 * seat_row) + seat_col
  41.  
  42. def get_missing_seat_id(a_boarding_passes: list[str]) -> int:
  43.     missing_seat_id: int = 0
  44.  
  45.     boarding_pass_seat_ids = [get_seating_id(boarding_pass) for boarding_pass in a_boarding_passes]
  46.     boarding_pass_seat_ids.sort()
  47.  
  48.     missing_seat_id = [
  49.         seat_id
  50.         for seat_id in boarding_pass_seat_ids
  51.         if ((seat_id + 1 not in boarding_pass_seat_ids
  52.              and seat_id != boarding_pass_seat_ids[-1]))
  53.     ]
  54.  
  55.     return missing_seat_id[0] + 1
  56.  
  57. boarding_passes = get_puzzle_data('aoc_2020/aoc_2020_day_05.txt')
  58. # print(f'{boarding_passes=}')
  59.  
  60. print(f"p1={max([get_seating_id(boarding_pass) for boarding_pass in boarding_passes])}")    
  61. print(f"p2={get_missing_seat_id(boarding_passes)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement