aoc/05/05_translate.py

25 lines
834 B
Python

"""
Turns out int() accepts different bases than 10.
We can also translate a string using translate() and a table,
or maketrans(). #TIL
"""
with open("input.txt", "r") as f:
seats = f.read().split('\n')
def id(code):
trans = str.maketrans('FBLR', '0101')
return int(code.translate(trans), 2) # it's literally just binary lmao -- x8 just means x1000 so shift 3 places, and there are 3 digits in the colpart
ids = [id(seat) for seat in seats]
print(f"Part 1: {max(ids)}") # i forgot that max() existed whoops
"""
Also, instead of iterating over the entire list of IDs,
we can find the missing seat by simply summing them.
The missing seat will be excluded from the total.
"""
expected_total = sum( range(min(ids), max(ids)+1) ) # some seats are missing
actual_total = sum(ids)
print(f"Part 2: {expected_total - actual_total}")