Advertisement
Armandur

Advent of code 2020-12-05

Dec 5th, 2020
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #   Advent of code 2020-12-05
  2. #   Armandur
  3.  
  4.  
  5. rows = list(range(0, 128))
  6. columns = list(range(0, 8))
  7.  
  8.  
  9. def search(entry, seats):
  10.     if len(seats) > 1:
  11.         half = int(len(seats) / 2)
  12.         if entry[0] == "F" or entry[0] == "L":
  13.             return search(entry[1:], seats[:half])  # Keep first half, and run func again without first char in entry
  14.  
  15.         elif entry[0] == "B" or entry[0] == "R":
  16.             return search(entry[1:], seats[half:])  # Keep second half, and run func again without first char in entry
  17.     else:
  18.         return seats[0]                             # Only one element remaining
  19.  
  20.  
  21. seatIDs = []
  22. with open("5 - input", 'r') as file:
  23.     lines = file.readlines()
  24.     for line in lines:
  25.         row = search(line[:7], rows)        # Search for row with first 7 chars of the line/entry
  26.         column = search(line[7:], columns)  # Search for column with the last chars after first 7
  27.         seatIDs.append((row * 8) + column)  # Calculate and append ID to list
  28.  
  29. seatIDs.sort()      # Sort IDs in numerical order
  30. print(seatIDs[-1])  # Part 1 - Print the last ID in list, which will be the highest
  31.  
  32. #   Part 2 - One ID is missing, check all IDs in incremental order, check if the current seatID is equal to the last+1,
  33. #   if not it is missing.
  34.  
  35. last = seatIDs[0] - 1
  36. for seatID in seatIDs:
  37.     if seatID != last + 1:
  38.         print(seatID - 1)
  39.         break
  40.     last = seatID
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement