"""
Advent of Code 2021 - Day 05

Run with:
    python puzzles/day05.py inputs/day05.txt
"""

import pathlib
import sys
from typing import Dict, List, Tuple

from collections import defaultdict
from dataclasses import dataclass


@dataclass
class Location:
    x: int = 0
    y: int = 0

    def __post_init__(self):
        self.x = int(self.x)
        self.y = int(self.y)


@dataclass
class VolcanicVent:
    start: Location
    end: Location

    def __post_init__(self):
        if self.start.x == self.end.x or self.start.y == self.end.y:
            self.type = "vertical" if self.start.x == self.end.x else "horizontal"
        else:
            self.type = "diagnal"


class Chart:
    vents: List = None
    diagram: Dict = None

    def __init__(self, vents: List[VolcanicVent]) -> None:
        self.diagram = defaultdict(int)
        self.vents = vents
        self._generate_diagram()

    def _generate_diagram(self) -> None:
        """
        .......1..
        ..1....1..
        ..1....1..
        .......1..
        .112111211
        ..........
        ..........
        ..........
        ..........
        222111....      0,9 -> 5,9      0,9 -> 2,9
        """

        vents = self.vents.copy()
        for vent in vents:
            min_x = min(vent.start.x, vent.end.x)
            max_x = max(vent.start.x, vent.end.x)
            min_y = min(vent.start.y, vent.end.y)
            max_y = max(vent.start.y, vent.end.y)

            for x in range(min_x, max_x + 1):
                for y in range(min_y, max_y + 1):
                    if vent.type != "diagnal":
                        self.diagram[x, y] += 1
                    else:
                        pass

    def calculate_overlaps(self, max_depth: int = 1) -> int:
        return sum(depth_count > max_depth for depth_count in self.diagram.values())


def part1(inputs: List[int]) -> int:
    c = Chart(inputs)
    return c.calculate_overlaps()


def part2(inputs: List[int]) -> int:
    return False


def parse(inputs: str) -> List[int]:
    """Parse the input string"""
    results = []
    for line in inputs.split("\n"):
        start_end = line.split(" -> ")
        start_split = start_end[0].split(",")
        end_split = start_end[1].split(",")
        results.append(
            VolcanicVent(
                start=Location(x=start_split[0], y=start_split[1]),
                end=Location(x=end_split[0], y=end_split[1]),
            )
        )
    return results


def solve(path: str) -> Tuple[int, int]:
    """Solve the puzzle"""
    puzzle_input = parse(pathlib.Path(path).read_text().strip())
    part1_result = part1(puzzle_input)
    part2_result = part2(puzzle_input)

    return part1_result, part2_result


def main() -> None:
    for path in sys.argv[1:]:
        print(f"Input File: {path}")

        part1_result, part2_result = solve(path)

        print(f"Part 1 Result: {part1_result}")
        print(f"Part 2 Result: {part2_result}")


if __name__ == "__main__":
    main()