Started working out a formula to math this, but gave up and let the computer do it.
This commit is contained in:
IamTheFij 2023-12-06 09:40:51 -08:00
parent 71309f3672
commit 4ca74a8e4c
3 changed files with 92 additions and 0 deletions

2
d06/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 54 81 70 88
Distance: 446 1292 1035 1007

88
d06/main.py Normal file
View File

@ -0,0 +1,88 @@
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}")

2
d06/sample.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200