2018: d03: ex1: add solution

This commit is contained in:
Bruno BELANYI 2024-12-29 15:26:09 -05:00
parent 22e21e5c88
commit a035967ba6

51
2018/d03/ex1/ex1.py Executable file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from collections.abc import Iterator
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Claim(NamedTuple):
top_left: Point
size: Point
def points(self) -> Iterator[Point]:
for dx, dy in map(
Point._make, itertools.product(range(self.size.x), range(self.size.y))
):
yield Point(self.top_left.x + dx, self.top_left.y + dy)
def solve(input: str) -> int:
def parse_claim(input: str) -> Claim:
offset, size = input.split("@")[1].strip().split(": ")
return Claim(
Point(*map(int, offset.split(","))),
Point(*map(int, size.split("x"))),
)
def parse(input: list[str]) -> list[Claim]:
return [parse_claim(line) for line in input]
def claim_per_point(claims: list[Claim]) -> dict[Point, int]:
points = itertools.chain.from_iterable(map(Claim.points, claims))
return Counter(points)
claims = parse(input.splitlines())
return sum(count > 1 for count in claim_per_point(claims).values())
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()