diff --git a/2020/day5_1.py b/2020/day5_1.py index 54831df..76f6d72 100644 --- a/2020/day5_1.py +++ b/2020/day5_1.py @@ -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) \ No newline at end of file diff --git a/2020/day5_2.py b/2020/day5_2.py new file mode 100644 index 0000000..739d99e --- /dev/null +++ b/2020/day5_2.py @@ -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 \ No newline at end of file