diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-12-11 22:48:48 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-12-11 22:49:02 +0100 |
commit | 603fb558d957201754cc3e64004f83e2f9ff4745 (patch) | |
tree | 323f2fe223496beb212a99494d9df1729ac8ca43 /2022/09/puzzles.py | |
parent | 2c56a9baf17d15ae06bcb0199c02d27700019070 (diff) |
Add 2022 day 9 solutions
Diffstat (limited to '2022/09/puzzles.py')
-rw-r--r-- | 2022/09/puzzles.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/2022/09/puzzles.py b/2022/09/puzzles.py new file mode 100644 index 0000000..565a39c --- /dev/null +++ b/2022/09/puzzles.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +# START PART 1 +SEGMENTS = 2 +# END PART 1 START PART 2 +SEGMENTS = 10 +# END PART 2 + +position = tuple[int, int] + + +class Segment: + def __init__(self): + self.x = 0 + self.y = 0 + + def track(self, pos: position) -> None: + x, y = pos + dx = x - self.x + dy = y - self.y + nx = 1 if dx > 0 else -1 + ny = 1 if dy > 0 else -1 + + match (abs(dx), abs(dy)): + case (2, 0): + self.x += nx + case (0, 2): + self.y += ny + case (2, 1) | (2, 2) | (1, 2): + self.x += nx + self.y += ny + + @property + def pos(self) -> position: + return self.x, self.y + + +def main() -> None: + with open("input", "r") as f: + data = [(xs[0], int(xs[1])) for xs in [l.split() for l in f.readlines()]] + + snake = [Segment() for _ in range(SEGMENTS)] + head = snake[0] + tail = snake[-1] + locs: set[position] = set() + + for d, c in data: + for _ in range(c): + match d: + case "U": + head.y += 1 + case "D": + head.y -= 1 + case "R": + head.x += 1 + case "L": + head.x -= 1 + + for i, segment in enumerate(snake[1:]): + segment.track(snake[i].pos) + + locs.add(tail.pos) + + print(len(locs)) + + +if __name__ == "__main__": + main() |