Shorten 2020 Day 05

This commit is contained in:
Akumatic 2020-12-05 11:58:01 +01:00
parent a99d739a9d
commit b868f63060
2 changed files with 7 additions and 17 deletions

View File

@ -5,29 +5,19 @@
def readFile() -> list:
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
return [line[:-1] for line in f.readlines()]
def parse(seat) -> tuple:
return sum([2**(6-i) for i in range(7) if seat[i] in ("B")]), \
sum([2**(9-i) for i in range(7, 10) if seat[i] in ("R")])
return [int(line[:-1].replace("F", "0").replace("B", "1")
.replace("L", "0").replace("R", "1"), 2) for line in f.readlines()]
def part1(seat_ids: list) -> int:
return max(seat_ids)
def part2(seat_ids: list) -> int:
for i in range(8, 1015): # front and back row skipped
for i in range(min(seat_ids), max(seat_ids)):
if i not in seat_ids and (i - 1) in seat_ids and (i + 1) in seat_ids:
return i
def test():
assert parse("FBFBBFFRLR") == (44,5)
assert parse("BFFFBBFRRR") == (70,7)
assert parse("FFFBBBFRRR") == (14,7)
assert parse("BBFFBBFRLL") == (102,4)
if __name__ == "__main__":
test()
seats = [parse(val) for val in readFile()]
seat_ids = [seat[0]*8 + seat[1] for seat in seats]
seat_ids = readFile()
print(f"Part 1: {part1(seat_ids)}")
print(f"Part 2: {part2(seat_ids)}")
print(f"Part 2: {part2(seat_ids)}")

View File

@ -17,7 +17,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
| --- | --- | --- |---| --- | --- | --- |
| 01 | :white_check_mark: | :white_check_mark: || 02 | :white_check_mark: | :white_check_mark: |
| 03 | :white_check_mark: | :white_check_mark: || 04 | :white_check_mark: | :white_check_mark: |
| 05 | | || 06 | | |
| 05 | :white_check_mark: | :white_check_mark: || 06 | | |
| 07 | | || 08 | | |
| 09 | | || 10 | | |
| 11 | | || 12 | | |