diff options
Diffstat (limited to 'day9')
| -rw-r--r-- | day9/__init__.py | 13 | ||||
| -rw-r--r-- | day9/test_init.py | 25 |
2 files changed, 34 insertions, 4 deletions
diff --git a/day9/__init__.py b/day9/__init__.py index 4846468..e0f1d61 100644 --- a/day9/__init__.py +++ b/day9/__init__.py | |||
| @@ -59,16 +59,21 @@ class Assignment(BaseAssignment, ABC): | |||
| 59 | def next_knot(head: Coordinate, tail: Coordinate): | 59 | def next_knot(head: Coordinate, tail: Coordinate): |
| 60 | delta = head - tail | 60 | delta = head - tail |
| 61 | 61 | ||
| 62 | if abs(delta.x) > 1 and delta.y == 0: | 62 | if abs(delta.x) < 2 and abs(delta.y) < 2: |
| 63 | pass | ||
| 64 | elif abs(delta.x) > 1 and delta.y == 0: | ||
| 63 | tail.x += delta.x - delta.polarity_x | 65 | tail.x += delta.x - delta.polarity_x |
| 64 | if abs(delta.x) > 1 and abs(delta.y) == 1: | 66 | elif abs(delta.x) > 1 and abs(delta.y) == 1: |
| 65 | tail.x += delta.x - delta.polarity_x | 67 | tail.x += delta.x - delta.polarity_x |
| 66 | tail.y += delta.y | 68 | tail.y += delta.y |
| 67 | if abs(delta.y) > 1 and delta.x == 0: | 69 | elif abs(delta.y) > 1 and delta.x == 0: |
| 68 | tail.y += delta.y - delta.polarity_y | 70 | tail.y += delta.y - delta.polarity_y |
| 69 | if abs(delta.y) > 1 and abs(delta.x) == 1: | 71 | elif abs(delta.y) > 1 and abs(delta.x) == 1: |
| 70 | tail.y += delta.y - delta.polarity_y | 72 | tail.y += delta.y - delta.polarity_y |
| 71 | tail.x += delta.x | 73 | tail.x += delta.x |
| 74 | elif abs(delta.x) > 1 and abs(delta.y) > 1: | ||
| 75 | tail.x += delta.x - delta.polarity_x | ||
| 76 | tail.y += delta.y - delta.polarity_y | ||
| 72 | 77 | ||
| 73 | def tail_positions(self, input: Iterator[str], length: int) -> Iterator[Coordinate]: | 78 | def tail_positions(self, input: Iterator[str], length: int) -> Iterator[Coordinate]: |
| 74 | knots = [Coordinate(0, 0) for _ in range(length)] | 79 | knots = [Coordinate(0, 0) for _ in range(length)] |
diff --git a/day9/test_init.py b/day9/test_init.py index eb889c7..431dbbb 100644 --- a/day9/test_init.py +++ b/day9/test_init.py | |||
| @@ -75,6 +75,31 @@ class TestAssignment: | |||
| 75 | assignment.visualize(range(1), range(-5, 1), unique_positions) == expected | 75 | assignment.visualize(range(1), range(-5, 1), unique_positions) == expected |
| 76 | ) | 76 | ) |
| 77 | 77 | ||
| 78 | def test_diagonal(self): | ||
| 79 | expected = "\n".join( | ||
| 80 | [ | ||
| 81 | "..#", | ||
| 82 | ".#.", | ||
| 83 | "s..", | ||
| 84 | ] | ||
| 85 | ) | ||
| 86 | |||
| 87 | input = [ | ||
| 88 | "R 1", | ||
| 89 | "U 1", | ||
| 90 | "R 1", | ||
| 91 | "U 1", | ||
| 92 | "R 1", | ||
| 93 | "U 1", | ||
| 94 | "R 1", | ||
| 95 | "U 1", | ||
| 96 | ] | ||
| 97 | |||
| 98 | assignment = day9.AssignmentOne(path=os.path.dirname(day9.__file__)) | ||
| 99 | unique_positions = assignment.unique_tail_positions(input=input, length=2) | ||
| 100 | |||
| 101 | assert assignment.visualize(range(3), range(3), unique_positions) == expected | ||
| 102 | |||
| 78 | 103 | ||
| 79 | class TestAssignmentOne: | 104 | class TestAssignmentOne: |
| 80 | def test_output_visualization(self): | 105 | def test_output_visualization(self): |
