2022: d18: ex1: add solution

This commit is contained in:
Bruno BELANYI 2022-12-18 12:44:40 +01:00
parent ca6a3885b0
commit eb007342f2

43
2022/d18/ex1/ex1.py Executable file
View file

@ -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()