From c7538c902a5e000448e5985bde7c6b7f3d7d60d3 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 31 Dec 2024 00:47:24 -0500 Subject: [PATCH] 2018: d23: ex1: add solution --- 2018/d23/ex1/ex1.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 2018/d23/ex1/ex1.py diff --git a/2018/d23/ex1/ex1.py b/2018/d23/ex1/ex1.py new file mode 100755 index 0000000..34c2d4c --- /dev/null +++ b/2018/d23/ex1/ex1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + z: int + + +class NanoBot(NamedTuple): + pos: Point + r: int + + +def solve(input: str) -> int: + def parse_nanobot(input: str) -> NanoBot: + pos, r = input.split(", ") + pos = pos.removeprefix("pos=<").removesuffix(">") + r = r.removeprefix("r=") + return NanoBot(Point(*(int(n) for n in pos.split(","))), int(r)) + + def parse(input: list[str]) -> list[NanoBot]: + return [parse_nanobot(line) for line in input] + + def dist(lhs: Point, rhs: Point) -> int: + return sum(abs(l - r) for l, r in zip(lhs, rhs)) + + bots = parse(input.splitlines()) + strongest = max(bots, key=lambda b: b.r) + return sum(dist(strongest.pos, bot.pos) <= strongest.r for bot in bots) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()