This commit is contained in:
Jonas Neugebauer 2020-12-11 09:42:17 +01:00
parent da703a6386
commit 5c60259457
2 changed files with 75 additions and 1 deletions

View File

@ -1,4 +1,38 @@
from math import floor, ceil
input = []
with open('day5_input.txt', 'r') as f:
for line in f.readlines():
input.append(line.strip())
input.append(line.strip())
def get_row( r, low=0, high=127 ):
if len(r) == 0 or low == high:
return high
else:
if r[0] == 'F':
return get_row(r[1:], low, floor((high+low)/2))
else:
return get_row(r[1:], ceil((high+low)/2), high)
def get_col( c, low=0, high=7 ):
if len(c) == 0 or low == high:
return high
else:
if c[0] == 'L':
return get_col(c[1:], low, floor((high+low)/2))
else:
return get_col(c[1:], ceil((high+low)/2), high)
def get_seat( bpass ):
row = get_row(bpass[:7])
col = get_col(bpass[7:])
return (row, col, row*8+col)
max_seat = 0
for bpass in input:
seat = get_seat(bpass)
if seat[2] > max_seat:
max_seat = seat[2]
print(max_seat)

40
2020/day5_2.py Normal file
View File

@ -0,0 +1,40 @@
from math import floor, ceil
input = []
with open('day5_input.txt', 'r') as f:
for line in f.readlines():
input.append(line.strip())
def get_row( r, low=0, high=127 ):
if len(r) == 0 or low == high:
return high
else:
if r[0] == 'F':
return get_row(r[1:], low, floor((high+low)/2))
else:
return get_row(r[1:], ceil((high+low)/2), high)
def get_col( c, low=0, high=7 ):
if len(c) == 0 or low == high:
return high
else:
if c[0] == 'L':
return get_col(c[1:], low, floor((high+low)/2))
else:
return get_col(c[1:], ceil((high+low)/2), high)
def get_seat( bpass ):
row = get_row(bpass[:7])
col = get_col(bpass[7:])
return (row, col, row*8+col)
seats = []
for bpass in input:
seats.append(get_seat(bpass))
seats.sort(key=lambda s: s[2])
for i in range(len(seats)-1):
if seats[i][2]+1 != seats[i+1][2]:
print(seats[i][2]+1)
break