diff options
Diffstat (limited to 'day9')
| -rw-r--r-- | day9/__init__.py | 108 | ||||
| -rw-r--r-- | day9/example.txt | 8 | ||||
| -rw-r--r-- | day9/input.txt | 2000 |
3 files changed, 2116 insertions, 0 deletions
diff --git a/day9/__init__.py b/day9/__init__.py new file mode 100644 index 0000000..afa44aa --- /dev/null +++ b/day9/__init__.py | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from copy import copy | ||
| 4 | from dataclasses import dataclass | ||
| 5 | from typing import Tuple, Iterator, List | ||
| 6 | |||
| 7 | from aoc import BaseAssignment | ||
| 8 | |||
| 9 | |||
| 10 | @dataclass | ||
| 11 | class Coordinate: | ||
| 12 | x: int | ||
| 13 | y: int | ||
| 14 | |||
| 15 | def __hash__(self): | ||
| 16 | return tuple([self.x, self.y]).__hash__() | ||
| 17 | |||
| 18 | def __sub__(self, other: "Coordinate"): | ||
| 19 | return Coordinate(self.x - other.x, self.y - other.y) | ||
| 20 | |||
| 21 | @property | ||
| 22 | def polarity_x(self): | ||
| 23 | try: | ||
| 24 | return abs(self.x) / self.x | ||
| 25 | except ZeroDivisionError: | ||
| 26 | return 0 | ||
| 27 | |||
| 28 | @property | ||
| 29 | def polarity_y(self): | ||
| 30 | try: | ||
| 31 | return abs(self.y) / self.y | ||
| 32 | except ZeroDivisionError: | ||
| 33 | return 0 | ||
| 34 | |||
| 35 | |||
| 36 | class Assignment(BaseAssignment, ABC): | ||
| 37 | def parse_line(self, item: str) -> Tuple[str, int]: | ||
| 38 | direction, amount = item.split(" ") | ||
| 39 | return direction, int(amount) | ||
| 40 | |||
| 41 | def move(self, direction: str, amount: int): | ||
| 42 | pass | ||
| 43 | |||
| 44 | @staticmethod | ||
| 45 | def next_head(head: Coordinate, direction: str): | ||
| 46 | match direction: | ||
| 47 | case "R": | ||
| 48 | head.x += 1 | ||
| 49 | case "L": | ||
| 50 | head.x -= 1 | ||
| 51 | case "U": | ||
| 52 | head.y += 1 | ||
| 53 | case "D": | ||
| 54 | head.y -= 1 | ||
| 55 | |||
| 56 | @staticmethod | ||
| 57 | def next_tail(head: Coordinate, tail: Coordinate): | ||
| 58 | delta = head - tail | ||
| 59 | |||
| 60 | if abs(delta.x) > 1 and delta.y == 0: | ||
| 61 | tail.x += delta.x - delta.polarity_x | ||
| 62 | if abs(delta.x) > 1 and abs(delta.y) == 1: | ||
| 63 | tail.x += delta.x - delta.polarity_x | ||
| 64 | tail.y += delta.y | ||
| 65 | if abs(delta.y > 1) and delta.x == 0: | ||
| 66 | tail.y += delta.y - delta.polarity_y | ||
| 67 | if abs(delta.y) > 1 and abs(delta.x) == 1: | ||
| 68 | tail.y += delta.y - delta.polarity_y | ||
| 69 | tail.x += delta.x | ||
| 70 | |||
| 71 | def tail_positions(self, input: Iterator[str]) -> Iterator[Coordinate]: | ||
| 72 | head = Coordinate(0, 0) | ||
| 73 | tail = Coordinate(0, 0) | ||
| 74 | |||
| 75 | for line in input: | ||
| 76 | direction, amount = self.parse_line(line) | ||
| 77 | |||
| 78 | for _ in range(amount): | ||
| 79 | self.next_head(head, direction) | ||
| 80 | self.next_tail(head, tail) | ||
| 81 | yield tail | ||
| 82 | |||
| 83 | |||
| 84 | class AssignmentOne(Assignment): | ||
| 85 | example_result = 13 | ||
| 86 | |||
| 87 | def run(self, input: Iterator) -> int: | ||
| 88 | unique_tail_positions = set() | ||
| 89 | |||
| 90 | for position in self.tail_positions(input): | ||
| 91 | unique_tail_positions.add(copy(position)) | ||
| 92 | |||
| 93 | print(unique_tail_positions) | ||
| 94 | |||
| 95 | for y in range(5): | ||
| 96 | items = [] | ||
| 97 | for x in range(6): | ||
| 98 | if Coordinate(x, y) in unique_tail_positions: | ||
| 99 | items.append("#") | ||
| 100 | else: | ||
| 101 | items.append(".") | ||
| 102 | print("".join(items)) | ||
| 103 | |||
| 104 | return len(unique_tail_positions) | ||
| 105 | |||
| 106 | |||
| 107 | class AssignmentTwo(Assignment): | ||
| 108 | pass | ||
diff --git a/day9/example.txt b/day9/example.txt new file mode 100644 index 0000000..9874df2 --- /dev/null +++ b/day9/example.txt | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | R 4 | ||
| 2 | U 4 | ||
| 3 | L 3 | ||
| 4 | D 1 | ||
| 5 | R 4 | ||
| 6 | D 1 | ||
| 7 | L 5 | ||
| 8 | R 2 | ||
diff --git a/day9/input.txt b/day9/input.txt new file mode 100644 index 0000000..3da93c3 --- /dev/null +++ b/day9/input.txt | |||
| @@ -0,0 +1,2000 @@ | |||
| 1 | L 2 | ||
| 2 | U 2 | ||
| 3 | L 1 | ||
| 4 | D 1 | ||
| 5 | U 1 | ||
| 6 | L 2 | ||
| 7 | R 2 | ||
| 8 | D 1 | ||
| 9 | U 2 | ||
| 10 | D 1 | ||
| 11 | R 2 | ||
| 12 | U 1 | ||
| 13 | L 2 | ||
| 14 | D 2 | ||
| 15 | L 2 | ||
| 16 | R 2 | ||
| 17 | L 2 | ||
| 18 | R 1 | ||
| 19 | L 2 | ||
| 20 | D 1 | ||
| 21 | U 1 | ||
| 22 | L 2 | ||
| 23 | U 1 | ||
| 24 | D 1 | ||
| 25 | R 2 | ||
| 26 | L 1 | ||
| 27 | R 1 | ||
| 28 | L 2 | ||
| 29 | U 2 | ||
| 30 | D 2 | ||
| 31 | U 1 | ||
| 32 | D 2 | ||
| 33 | R 1 | ||
| 34 | D 1 | ||
| 35 | R 2 | ||
| 36 | L 2 | ||
| 37 | D 1 | ||
| 38 | L 1 | ||
| 39 | U 2 | ||
| 40 | R 2 | ||
| 41 | L 2 | ||
| 42 | U 2 | ||
| 43 | D 1 | ||
| 44 | U 2 | ||
| 45 | D 2 | ||
| 46 | R 1 | ||
| 47 | U 2 | ||
| 48 | L 2 | ||
| 49 | D 2 | ||
| 50 | U 2 | ||
| 51 | D 2 | ||
| 52 | L 2 | ||
| 53 | R 1 | ||
| 54 | L 2 | ||
| 55 | D 2 | ||
| 56 | U 1 | ||
| 57 | R 2 | ||
| 58 | U 2 | ||
| 59 | L 1 | ||
| 60 | D 2 | ||
| 61 | R 2 | ||
| 62 | L 2 | ||
| 63 | R 1 | ||
| 64 | L 2 | ||
| 65 | D 1 | ||
| 66 | R 2 | ||
| 67 | L 2 | ||
| 68 | R 1 | ||
| 69 | L 2 | ||
| 70 | D 1 | ||
| 71 | U 1 | ||
| 72 | L 1 | ||
| 73 | D 1 | ||
| 74 | R 2 | ||
| 75 | D 1 | ||
| 76 | L 1 | ||
| 77 | D 1 | ||
| 78 | R 1 | ||
| 79 | D 1 | ||
| 80 | U 2 | ||
| 81 | R 1 | ||
| 82 | L 1 | ||
| 83 | U 2 | ||
| 84 | L 2 | ||
| 85 | D 2 | ||
| 86 | L 1 | ||
| 87 | U 1 | ||
| 88 | D 1 | ||
| 89 | L 1 | ||
| 90 | U 1 | ||
| 91 | D 1 | ||
| 92 | U 1 | ||
| 93 | R 2 | ||
| 94 | L 2 | ||
| 95 | U 2 | ||
| 96 | R 2 | ||
| 97 | L 2 | ||
| 98 | D 2 | ||
| 99 | U 1 | ||
| 100 | L 1 | ||
| 101 | R 1 | ||
| 102 | U 2 | ||
| 103 | R 1 | ||
| 104 | D 2 | ||
| 105 | R 1 | ||
| 106 | L 2 | ||
| 107 | R 2 | ||
| 108 | U 2 | ||
| 109 | D 2 | ||
| 110 | R 1 | ||
| 111 | U 2 | ||
| 112 | L 2 | ||
| 113 | R 1 | ||
| 114 | L 2 | ||
| 115 | D 3 | ||
| 116 | L 3 | ||
| 117 | D 3 | ||
| 118 | L 3 | ||
| 119 | U 3 | ||
| 120 | D 2 | ||
| 121 | R 2 | ||
| 122 | D 3 | ||
| 123 | L 1 | ||
| 124 | R 2 | ||
| 125 | D 2 | ||
| 126 | R 2 | ||
| 127 | D 3 | ||
| 128 | U 3 | ||
| 129 | D 1 | ||
| 130 | R 2 | ||
| 131 | D 3 | ||
| 132 | L 1 | ||
| 133 | D 1 | ||
| 134 | L 1 | ||
| 135 | D 3 | ||
| 136 | R 2 | ||
| 137 | U 2 | ||
| 138 | D 3 | ||
| 139 | R 2 | ||
| 140 | U 2 | ||
| 141 | |||
