Advent of Code 2015 day 14 part 1: solved

This commit is contained in:
Fabian Tessmer 2025-03-29 09:00:51 +01:00
parent dce8869d9f
commit 1cb4e884f0
4 changed files with 87 additions and 3 deletions

View File

@ -75,6 +75,6 @@ if __name__ == '__main__':
puzzle_input += open("self_input.txt", "r").read().splitlines()
converted_input = load_puzzle_input(puzzle_input)
print(converted_input)
print("Part2: ", get_perfect_seating(converted_input))
print("solution: ", )
print("Part2: ")
print("solution: ", get_perfect_seating(converted_input))

9
2015/day14/input.txt Normal file
View File

@ -0,0 +1,9 @@
Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.

0
2015/day14/problem.txt Normal file
View File

75
2015/day14/solution.py Normal file
View File

@ -0,0 +1,75 @@
class Reindeer:
def __init__(self, name: str, speed: int, sprint_duration: int, rest_duration: int) -> None:
self.name = name
self.speed = speed
self.sprint_duration = sprint_duration
self.rest_duration = rest_duration
self.currently_sprinting = True
self.current_duration = sprint_duration
self.distance = 0
def step(self) -> None:
if self.currently_sprinting and self.current_duration >= 1:
self.current_duration -= 1
self.distance += self.speed
elif not self.currently_sprinting and self.current_duration >= 1:
self.current_duration -= 1
else:
self.currently_sprinting = not self.currently_sprinting
if self.currently_sprinting:
self.current_duration = self.sprint_duration
else:
self.current_duration = self.rest_duration
self.step()
def get_distance(self) -> int:
return self.distance
class Race:
def __init__(self):
self.reindeers = []
def load_reindeers(self, reindeers: list) -> None:
for reindeer in reindeers:
name, rest = reindeer.split(" can fly ")
speed, rest = rest.split(" km/s for ")
sprint_duration, rest = rest.split(" seconds, but then must rest for ")
rest_duration = rest.split(" ")[0]
speed = int(speed)
sprint_duration = int(sprint_duration)
rest_duration = int(rest_duration)
self.reindeers.append(Reindeer(name, speed, sprint_duration, rest_duration))
def run_race(self, ticks) -> int:
for i in range(ticks):
for reindeer in self.reindeers:
reindeer.step()
max_distance = 0
for reindeer in self.reindeers:
distance = reindeer.get_distance()
if distance > max_distance:
max_distance = distance
return max_distance
if __name__ == '__main__':
test_input = [
"Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
"Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."
]
test_race = Race()
test_race.load_reindeers(test_input)
assert test_race.run_race(1000) == 1120, "Error, Example couldn't compute"
print("All test passed")
puzzle_input = open("input.txt", "r").read().splitlines()
race = Race()
race.load_reindeers(puzzle_input)
print("solution: ", race.run_race(2503))
print("Part2: ")
print("solution: ", )