diff --git a/d06/input.txt b/d06/input.txt new file mode 100644 index 0000000..f1837b6 --- /dev/null +++ b/d06/input.txt @@ -0,0 +1,2 @@ +Time: 54 81 70 88 +Distance: 446 1292 1035 1007 diff --git a/d06/main.py b/d06/main.py new file mode 100644 index 0000000..aaa1f01 --- /dev/null +++ b/d06/main.py @@ -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}") diff --git a/d06/sample.txt b/d06/sample.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/d06/sample.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200