From 2890aac81e37c72547a01a7e589343b12a9da46e Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 3 Jun 2026 19:49:04 +0200 Subject: [PATCH] 2015 day 14 --- adventofcode/2015/day14/day14.py | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 adventofcode/2015/day14/day14.py diff --git a/adventofcode/2015/day14/day14.py b/adventofcode/2015/day14/day14.py new file mode 100644 index 0000000..7be399a --- /dev/null +++ b/adventofcode/2015/day14/day14.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +import fileinput +import re +from dataclasses import dataclass + + +@dataclass +class Reeinder: + name: str + speed: int + duration: int + rest_duration: int + cnt: int = 0 + total_distance: int = 0 + is_flying: bool = True + score = 0 + + def __post_init__(self): + self.cnt = self.duration + + +def main(inp): + data = {x.split()[0]: tuple(map(int, re.findall(r"(\d+)", x))) for x in inp} + simulation = [ + Reeinder(name, speed, duration, rest_duration) + for name, (speed, duration, rest_duration) in data.items() + ] + max_steps = 2503 + for _ in range(max_steps): + for reeinder in simulation: + if reeinder.is_flying: + reeinder.total_distance += reeinder.speed + reeinder.cnt -= 1 + if reeinder.cnt == 0: + reeinder.is_flying = False + reeinder.cnt = reeinder.rest_duration + else: + reeinder.cnt -= 1 + if reeinder.cnt == 0: + reeinder.is_flying = True + reeinder.cnt = reeinder.duration + # for part 2, score the first reeinder at each simulation step + max_dist = max(x.total_distance for x in simulation) + for reeinder in simulation: + if reeinder.total_distance == max_dist: + reeinder.score += 1 + + print("Part 1:", max(x.total_distance for x in simulation)) + print("Part 2:", max(x.score for x in simulation)) + + +if __name__ == "__main__": + lines = [x.rstrip() for x in fileinput.input()] + main(lines)