Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Advent of code 2020-12-05
- # Armandur
- rows = list(range(0, 128))
- columns = list(range(0, 8))
- def search(entry, seats):
- if len(seats) > 1:
- half = int(len(seats) / 2)
- if entry[0] == "F" or entry[0] == "L":
- return search(entry[1:], seats[:half]) # Keep first half, and run func again without first char in entry
- elif entry[0] == "B" or entry[0] == "R":
- return search(entry[1:], seats[half:]) # Keep second half, and run func again without first char in entry
- else:
- return seats[0] # Only one element remaining
- seatIDs = []
- with open("5 - input", 'r') as file:
- lines = file.readlines()
- for line in lines:
- row = search(line[:7], rows) # Search for row with first 7 chars of the line/entry
- column = search(line[7:], columns) # Search for column with the last chars after first 7
- seatIDs.append((row * 8) + column) # Calculate and append ID to list
- seatIDs.sort() # Sort IDs in numerical order
- print(seatIDs[-1]) # Part 1 - Print the last ID in list, which will be the highest
- # Part 2 - One ID is missing, check all IDs in incremental order, check if the current seatID is equal to the last+1,
- # if not it is missing.
- last = seatIDs[0] - 1
- for seatID in seatIDs:
- if seatID != last + 1:
- print(seatID - 1)
- break
- last = seatID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement