From a57da6fcfeaba8bd58126ce27caba39f6a4b36d1 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 18 Dec 2022 12:44:40 +0100 Subject: [PATCH] 2022: d18: ex1: add solution --- 2022/d18/ex1/ex1.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 2022/d18/ex1/ex1.py diff --git a/2022/d18/ex1/ex1.py b/2022/d18/ex1/ex1.py new file mode 100755 index 0000000..71a8734 --- /dev/null +++ b/2022/d18/ex1/ex1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +import sys +from collections.abc import Iterator +from typing import NamedTuple + + +class Point3(NamedTuple): + x: int + y: int + z: int + + @classmethod + def from_input(cls, input: str) -> "Point3": + x, y, z = map(int, input.split(",")) + return cls(x, y, z) + + @classmethod + def neighbours(cls, p: "Point3") -> Iterator["Point3"]: + for dx, dy, dz in ( + (0, 0, -1), + (0, 0, 1), + (0, -1, 0), + (0, 1, 0), + (-1, 0, 0), + (1, 0, 0), + ): + yield cls(p.x + dx, p.y + dy, p.z + dz) + + +def solve(input: list[str]) -> int: + cubes = {Point3.from_input(line) for line in input} + + return sum(1 for p in cubes for n in Point3.neighbours(p) if n not in cubes) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()