diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-15 02:14:44 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-15 02:14:44 +0100 |
commit | 86df18e92ea6c509ef8bfa333a7279f65c09900a (patch) | |
tree | a94560f9ed914c91f5d54fcf809cc8d96b440b5d | |
parent | c15ce118475359c87f118646514291eeec08ceff (diff) |
Add 2019 day 3 solutions
-rw-r--r-- | 2019/03/.gitignore | 1 | ||||
-rw-r--r-- | 2019/03/Makefile | 1 | ||||
-rw-r--r-- | 2019/03/puzzles.py | 58 |
3 files changed, 60 insertions, 0 deletions
diff --git a/2019/03/.gitignore b/2019/03/.gitignore new file mode 100644 index 0000000..8931d44 --- /dev/null +++ b/2019/03/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py
\ No newline at end of file diff --git a/2019/03/Makefile b/2019/03/Makefile new file mode 100644 index 0000000..6e58a9d --- /dev/null +++ b/2019/03/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/py.mk
\ No newline at end of file diff --git a/2019/03/puzzles.py b/2019/03/puzzles.py new file mode 100644 index 0000000..5588a02 --- /dev/null +++ b/2019/03/puzzles.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +from typing import NamedTuple, Self + + +class Vec2(NamedTuple): + x: int + y: int + + def __add__(self, other: Self) -> Self: + return Vec2(self.x + other.x, self.y + other.y) + + def __sub__(self, other: Self) -> Self: + return Vec2(self.x - other.x, self.y - other.y) + + +def main() -> None: + with open("input", "r") as f: + data = [line.rstrip().split(',') for line in f.readlines()] + + p1 = plot(data[0]) + p2 = plot(data[1]) + + # START PART 1 + p1 = set(p1.keys()) + p2 = set(p2.keys()) + print(min(abs(x) + abs(y) for x, y in p1 & p2)) + # END PART 1 START PART 2 + print(min(p1[k] + p2[k] for k in p1.keys() if k in p2)) + # END PART 2 + + +def plot(data: list[str]) -> dict[Vec2, int]: + t = 0 + p = Vec2(0, 0) + s: dict[Vec2, int] = {} + + VMAP = { + 'U': Vec2(0, +1), + 'D': Vec2(0, -1), + 'L': Vec2(-1, 0), + 'R': Vec2(+1, 0), + } + + for spec in data: + d, n = spec[0], int(spec[1:]) + v⃗ = VMAP[d] + for _ in range(n): + t += 1 + p += v⃗ + s[p] = t + + return s + + + +if __name__ == "__main__": + main()
\ No newline at end of file |