advent-of-code/2022/d18/ex1/ex1.py

44 lines
916 B
Python
Raw Normal View History

2022-12-18 12:44:40 +01:00
#!/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()