Browse Source

Day 5

main
Noëlle 3 years ago
parent
commit
1158eb09bb
2 changed files with 95 additions and 0 deletions
  1. 35
    0
      day5a.py
  2. 60
    0
      day5b.py

+ 35
- 0
day5a.py View File

@@ -0,0 +1,35 @@
class SeatPass:
def __init__(self, seatcode):
self.getrow(seatcode[:7])
self.getcol(seatcode[7:])
self.getseat()
def getrow(self, seatcode):
seatcode = seatcode.replace('F', '0')
seatcode = seatcode.replace('B', '1')
self.row = int(seatcode, 2)
def getcol(self, seatcode):
seatcode = seatcode.replace('L', '0')
seatcode = seatcode.replace('R', '1')
self.col = int(seatcode, 2)

def getseat(self):
self.seatnum = (self.row * 8) + self.col


def main():
with open('input5.txt') as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
passes = []
maxseat = -1
for line in lines:
p = SeatPass(line)
passes.append(p)
if p.seatnum > maxseat:
maxseat = p.seatnum
print(f"The largest seat number I've seen is {maxseat}.")

if __name__ == "__main__":
main()

+ 60
- 0
day5b.py View File

@@ -0,0 +1,60 @@
class SeatPass:
def __init__(self, seatcode):
self.getrow(seatcode[:7])
self.getcol(seatcode[7:])
self.getseat()
def getrow(self, seatcode):
seatcode = seatcode.replace('F', '0')
seatcode = seatcode.replace('B', '1')
self.row = int(seatcode, 2)
def getcol(self, seatcode):
seatcode = seatcode.replace('L', '0')
seatcode = seatcode.replace('R', '1')
self.col = int(seatcode, 2)

def getseat(self):
self.seatnum = (self.row * 8) + self.col

def findOutlier(passes, minrow, maxrow):
for i in range(1,len(passes)):
p = passes[i]
if p.row == minrow or p.row == maxrow:
continue
if p.seatnum + 1 == passes[i+1].seatnum:
continue
else:
return p
return False

def main():
with open('input5.txt') as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
passes = []
maxseat = -1
minrow = 999
maxrow = -1
for line in lines:
p = SeatPass(line)
passes.append(p)
if p.seatnum > maxseat:
maxseat = p.seatnum
if p.row > maxrow:
maxrow = p.row
if p.row < minrow:
minrow = p.row
passes.sort(key=lambda x: x.seatnum)
seats = sorted([p.seatnum for p in passes])
print(seats)
mypass = findOutlier(passes, minrow, maxrow)
if mypass == False:
print("I didn't find a pass meeting these criteria.")
else:
print(mypass.seatnum+1)
print(f"The largest seat number I've seen is {maxseat}.")

if __name__ == "__main__":
main()

Loading…
Cancel
Save