2022: d18: ex1: add solution
This commit is contained in:
parent
ca6a3885b0
commit
eb007342f2
43
2022/d18/ex1/ex1.py
Executable file
43
2022/d18/ex1/ex1.py
Executable 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()
|
Loading…
Reference in a new issue