rosien

Binary_boarding_day5

Feb 19th, 2021 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. with open ('Day5.txt', 'rt') as f:
  2.     data = f.read().strip()
  3. data = data.split('\n')
  4.  
  5. #part 1
  6. def find_position(code,  start, end, left, right):
  7.     for value in code:
  8.         if value == left:
  9.             end = (end + start)//2
  10.             final = end
  11.         else:
  12.             start = (start + end)//2 + 1
  13.             final = start
  14.     return final
  15.            
  16. def find_id(code):
  17.     rows = code[:7]
  18.     row_final = find_position(rows, 0, 127, 'F', 'B')
  19.     columns = code[7:]
  20.     col_final = find_position(columns, 0, 7, 'L', 'R')
  21.     return (row_final * 8 + col_final)
  22.            
  23.  
  24. max_id = 0
  25. for seat in data:
  26.     max_id = max(max_id, find_id(seat))
  27. print(max_id)
  28.  
  29.  
  30. #part 2
  31. seats = set()
  32. for seat in data:
  33.     seats.add(find_id(seat))
  34. all_seats = set()
  35. for i in range(min(seats), max(seats)+1):
  36.     all_seats.add(i)
  37. print(all_seats.difference(seats))
Add Comment
Please, Sign In to add comment