You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day5b.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class SeatPass:
  2. def __init__(self, seatcode):
  3. self.getrow(seatcode[:7])
  4. self.getcol(seatcode[7:])
  5. self.getseat()
  6. def getrow(self, seatcode):
  7. seatcode = seatcode.replace('F', '0')
  8. seatcode = seatcode.replace('B', '1')
  9. self.row = int(seatcode, 2)
  10. def getcol(self, seatcode):
  11. seatcode = seatcode.replace('L', '0')
  12. seatcode = seatcode.replace('R', '1')
  13. self.col = int(seatcode, 2)
  14. def getseat(self):
  15. self.seatnum = (self.row * 8) + self.col
  16. def findOutlier(passes, minrow, maxrow):
  17. for i in range(1,len(passes)):
  18. p = passes[i]
  19. if p.row == minrow or p.row == maxrow:
  20. continue
  21. if p.seatnum + 1 == passes[i+1].seatnum:
  22. continue
  23. else:
  24. return p
  25. return False
  26. def main():
  27. with open('input5.txt') as file:
  28. lines = file.readlines()
  29. lines = [line.strip() for line in lines]
  30. passes = []
  31. maxseat = -1
  32. minrow = 999
  33. maxrow = -1
  34. for line in lines:
  35. p = SeatPass(line)
  36. passes.append(p)
  37. if p.seatnum > maxseat:
  38. maxseat = p.seatnum
  39. if p.row > maxrow:
  40. maxrow = p.row
  41. if p.row < minrow:
  42. minrow = p.row
  43. passes.sort(key=lambda x: x.seatnum)
  44. seats = sorted([p.seatnum for p in passes])
  45. print(seats)
  46. mypass = findOutlier(passes, minrow, maxrow)
  47. if mypass == False:
  48. print("I didn't find a pass meeting these criteria.")
  49. else:
  50. print(mypass.seatnum+1)
  51. print(f"The largest seat number I've seen is {maxseat}.")
  52. if __name__ == "__main__":
  53. main()