Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open ('Day5.txt', 'rt') as f:
- data = f.read().strip()
- data = data.split('\n')
- #part 1
- def find_position(code, start, end, left, right):
- for value in code:
- if value == left:
- end = (end + start)//2
- final = end
- else:
- start = (start + end)//2 + 1
- final = start
- return final
- def find_id(code):
- rows = code[:7]
- row_final = find_position(rows, 0, 127, 'F', 'B')
- columns = code[7:]
- col_final = find_position(columns, 0, 7, 'L', 'R')
- return (row_final * 8 + col_final)
- max_id = 0
- for seat in data:
- max_id = max(max_id, find_id(seat))
- print(max_id)
- #part 2
- seats = set()
- for seat in data:
- seats.add(find_id(seat))
- all_seats = set()
- for i in range(min(seats), max(seats)+1):
- all_seats.add(i)
- print(all_seats.difference(seats))
Add Comment
Please, Sign In to add comment