74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
import aoclib
|
|
|
|
DAY = 5
|
|
|
|
|
|
def getSeatRowColumn(seat_string):
|
|
if len(seat_string) != 10:
|
|
return None
|
|
|
|
row_string = seat_string[:7]
|
|
column_string = seat_string[-3:]
|
|
|
|
rows = list(range(0, 128))
|
|
for row_char in row_string:
|
|
if row_char == 'F':
|
|
rows = rows[:int(len(rows) / 2)]
|
|
elif row_char == 'B':
|
|
rows = rows[-int(len(rows) / 2):]
|
|
|
|
if len(rows) > 1:
|
|
raise ValueError('left with more than one row: %s' % rows)
|
|
|
|
columns = list(range(0, 8))
|
|
for column_char in column_string:
|
|
if column_char == 'L':
|
|
columns = columns[:int(len(columns) / 2)]
|
|
elif column_char == 'R':
|
|
columns = columns[-int(len(columns) / 2):]
|
|
|
|
if len(columns) > 1:
|
|
raise ValueError('left with more than one column: %s' % columns)
|
|
|
|
return rows[0], columns[0]
|
|
|
|
|
|
def getSeatID(row, column):
|
|
return row * 8 + column
|
|
|
|
|
|
def part1(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=5, test=test_mode)
|
|
max_id = 0
|
|
for seat in my_input:
|
|
row, column = getSeatRowColumn(seat)
|
|
max_id = max(max_id, getSeatID(row, column))
|
|
|
|
return max_id
|
|
|
|
|
|
def part2(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=5, test=test_mode)
|
|
seat_list = list(range(0, 1024))
|
|
min_id = 1023
|
|
for seat in my_input:
|
|
row, column = getSeatRowColumn(seat)
|
|
seat_id = getSeatID(row, column)
|
|
min_id = min(min_id, seat_id)
|
|
seat_list.remove(seat_id)
|
|
|
|
for seat_id in seat_list:
|
|
if seat_id > min_id:
|
|
return seat_id
|
|
|
|
|
|
if __name__ == '__main__':
|
|
assert part1(test_mode=True) == 820, "Part 1 TEST FAILED"
|
|
aoclib.printSolution(DAY, 1, part1())
|
|
# sadly there's no real test input for part 2
|
|
# so just check for the expected outcome with the test set
|
|
# which has nothing to do with the problem description; it's just min(seat_id) + 1
|
|
assert part2(test_mode=True) == 120, "Part 2 TEST FAILED"
|
|
aoclib.printSolution(DAY, 2, part2())
|