Add code for advent day 5
This commit is contained in:
parent
948009891a
commit
4b1ed00750
3 changed files with 74 additions and 0 deletions
1
day_5/info.md
Normal file
1
day_5/info.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
https://adventofcode.com/2020/day/5
|
||||
34
day_5/part_1.py
Normal file
34
day_5/part_1.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
def binary_partition(instructions, area, lesser):
|
||||
# Base Case: no instructions remaining
|
||||
if len(instructions) == 0:
|
||||
if instructions == lesser:
|
||||
return area[0]
|
||||
else:
|
||||
return area[1]
|
||||
|
||||
# Recursive Case: Multiple instructions remaining
|
||||
mid = ((area[1] - area[0]) // 2) + area[0]
|
||||
if instructions[0] == lesser:
|
||||
return binary_partition(instructions[1:], [area[0], mid], lesser)
|
||||
else:
|
||||
return binary_partition(instructions[1:], [mid, area[1]], lesser)
|
||||
|
||||
|
||||
def row(instructions):
|
||||
return binary_partition(instructions, [0, 127], "F")
|
||||
|
||||
def col(instructions):
|
||||
return binary_partition(instructions, [0, 7], "L")
|
||||
|
||||
|
||||
with open("input") as f:
|
||||
data = f.read().split("\n")
|
||||
|
||||
seat_ids = []
|
||||
|
||||
for boarding_pass in data:
|
||||
r = row(boarding_pass[:-3])
|
||||
c = col(boarding_pass[-3:])
|
||||
seat_id = (r * 8) + c
|
||||
seat_ids.append(seat_id)
|
||||
print(f"Highest ID: {max(seat_ids)}")
|
||||
39
day_5/part_2.py
Normal file
39
day_5/part_2.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
def binary_partition(instructions, area, lesser):
|
||||
# Base Case: no instructions remaining
|
||||
if len(instructions) == 0:
|
||||
if instructions == lesser:
|
||||
return area[0]
|
||||
else:
|
||||
return area[1]
|
||||
|
||||
# Recursive Case: Multiple instructions remaining
|
||||
mid = ((area[1] - area[0]) // 2) + area[0]
|
||||
if instructions[0] == lesser:
|
||||
return binary_partition(instructions[1:], [area[0], mid], lesser)
|
||||
else:
|
||||
return binary_partition(instructions[1:], [mid, area[1]], lesser)
|
||||
|
||||
|
||||
def row(instructions):
|
||||
return binary_partition(instructions, [0, 127], "F")
|
||||
|
||||
def col(instructions):
|
||||
return binary_partition(instructions, [0, 7], "L")
|
||||
|
||||
|
||||
with open("input") as f:
|
||||
data = f.read().split("\n")
|
||||
|
||||
seat_ids = []
|
||||
|
||||
for boarding_pass in data:
|
||||
r = row(boarding_pass[:-3])
|
||||
c = col(boarding_pass[-3:])
|
||||
seat_id = (r * 8) + c
|
||||
seat_ids.append(seat_id)
|
||||
|
||||
seat_ids.sort()
|
||||
|
||||
for seat in seat_ids:
|
||||
if seat + 1 not in seat_ids and seat + 2 in seat_ids:
|
||||
print(f"Missing seat: {seat + 1}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue