From 321696264b02827988206685bad5fb7825774b9f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 11 May 2025 03:57:40 +0100 Subject: [PATCH] 2017: d20: ex1: add solution --- 2017/d20/ex1/ex1.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 2017/d20/ex1/ex1.py diff --git a/2017/d20/ex1/ex1.py b/2017/d20/ex1/ex1.py new file mode 100755 index 0000000..887f911 --- /dev/null +++ b/2017/d20/ex1/ex1.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + z: int + + +class Particle(NamedTuple): + pos: Point + vel: Point + acc: Point + + @classmethod + def from_str(cls, input: str) -> "Particle": + p, v, a = input.split(", ") + return cls( + Point(*map(int, p[3:-1].split(","))), + Point(*map(int, v[3:-1].split(","))), + Point(*map(int, a[3:-1].split(","))), + ) + + +def solve(input: str) -> int: + def parse(input: str) -> list[Particle]: + return [Particle.from_str(line) for line in input.splitlines()] + + def dist(point: Point, other: Point) -> int: + return sum(abs(a - b) for a, b in zip(point, other)) + + particles = parse(input) + orig = Point(0, 0, 0) + # Lowest acceleration will be closest to origin as time tends to infinity + # Same logic for velocity and position + closest_particle = min( + particles, + key=lambda p: (dist(p.acc, orig), dist(p.vel, orig), dist(p.pos, orig)), + ) + return particles.index(closest_particle) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()