aoc-2023/d06/main.py
Ian Fijolek 4ca74a8e4c Day 6
Started working out a formula to math this, but gave up and let the computer do it.
2023-12-06 09:40:51 -08:00

89 lines
2.0 KiB
Python

from pathlib import Path
class Boat:
def __init__(self, starting_speed: int, acceleration: int):
self.start_speed = starting_speed
self.acc = acceleration
def hold(self, hold_time: int, time_limit: int) -> int:
return (time_limit - hold_time) * ((self.acc * hold_time) + self.start_speed)
def part1(input: Path) -> int:
# Boat properties
start_speed = 0
acc = 1
boat = Boat(start_speed, acc)
with input.open() as f:
times = [int(v) for v in next(f).split()[1:]]
dists = [int(v) for v in next(f).split()[1:]]
answer = 1
for time_limit, target_distance in zip(times, dists):
times = [
hold_time
for hold_time in range(0, time_limit)
if boat.hold(hold_time, time_limit) > target_distance
]
answer *= len(times)
return answer
def solve_calculus():
# Gave up on this. Been too long since I studied linear algebra
# Boat properties
start_speed = 0
acc = 1
# Round properties
time_limit = 7
target_distance = 9
# Variable
hold_time = 1
# Formula
# Curve
d = (time_limit - hold_time) * ((acc * hold_time) + start_speed)
# Line
d = target_distance
target_distance = (time_limit - hold_time) * ((acc * hold_time) + start_speed)
return d
def part2(input: Path) -> int:
# Boat properties
start_speed = 0
acc = 1
boat = Boat(start_speed, acc)
with input.open() as f:
time_limit = int("".join([v for v in next(f).split()[1:]]))
target_distance = int("".join([v for v in next(f).split()[1:]]))
times = [
hold_time
for hold_time in range(0, time_limit)
if boat.hold(hold_time, time_limit) > target_distance
]
return len(times)
if __name__ == "__main__":
# input = Path("sample.txt")
input = Path("input.txt")
result1 = part1(input)
print(f"part1 {result1}")
result2 = part2(input)
print(f"part2 {result2}")