diff options
Diffstat (limited to 'day9')
| -rw-r--r-- | day9/__init__.py | 35 | ||||
| -rw-r--r-- | day9/test_init.py | 14 |
2 files changed, 37 insertions, 12 deletions
diff --git a/day9/__init__.py b/day9/__init__.py index afa44aa..f198deb 100644 --- a/day9/__init__.py +++ b/day9/__init__.py | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | from abc import ABC | 2 | from abc import ABC |
| 3 | from copy import copy | 3 | from copy import copy |
| 4 | from dataclasses import dataclass | 4 | from dataclasses import dataclass |
| 5 | from typing import Tuple, Iterator, List | 5 | from typing import Tuple, Iterator, List, Set |
| 6 | 6 | ||
| 7 | from aoc import BaseAssignment | 7 | from aoc import BaseAssignment |
| 8 | 8 | ||
| @@ -80,28 +80,39 @@ class Assignment(BaseAssignment, ABC): | |||
| 80 | self.next_tail(head, tail) | 80 | self.next_tail(head, tail) |
| 81 | yield tail | 81 | yield tail |
| 82 | 82 | ||
| 83 | 83 | def unique_tail_positions(self, input: Iterator[str]) -> Set[Coordinate]: | |
| 84 | class AssignmentOne(Assignment): | ||
| 85 | example_result = 13 | ||
| 86 | |||
| 87 | def run(self, input: Iterator) -> int: | ||
| 88 | unique_tail_positions = set() | 84 | unique_tail_positions = set() |
| 89 | 85 | ||
| 90 | for position in self.tail_positions(input): | 86 | for position in self.tail_positions(input): |
| 91 | unique_tail_positions.add(copy(position)) | 87 | unique_tail_positions.add(copy(position)) |
| 92 | 88 | ||
| 93 | print(unique_tail_positions) | 89 | return unique_tail_positions |
| 94 | 90 | ||
| 95 | for y in range(5): | 91 | def visualize(self, width: int, height: int, positions: Set[Coordinate]): |
| 92 | rows = [] | ||
| 93 | for y in range(height): | ||
| 96 | items = [] | 94 | items = [] |
| 97 | for x in range(6): | 95 | for x in range(width): |
| 98 | if Coordinate(x, y) in unique_tail_positions: | 96 | if x == 0 and y == 0: |
| 97 | items.append("s") | ||
| 98 | elif Coordinate(x, y) in positions: | ||
| 99 | items.append("#") | 99 | items.append("#") |
| 100 | else: | 100 | else: |
| 101 | items.append(".") | 101 | items.append(".") |
| 102 | print("".join(items)) | 102 | rows.append(items) |
| 103 | |||
| 104 | return "\n".join(["".join(row) for row in reversed(rows)]) | ||
| 105 | |||
| 106 | |||
| 107 | class AssignmentOne(Assignment): | ||
| 108 | example_result = 13 | ||
| 109 | |||
| 110 | def run(self, input: Iterator) -> int: | ||
| 111 | unique_positions = self.unique_tail_positions(input) | ||
| 112 | print() | ||
| 113 | print(self.visualize(6, 5, unique_positions)) | ||
| 103 | 114 | ||
| 104 | return len(unique_tail_positions) | 115 | return len(unique_positions) |
| 105 | 116 | ||
| 106 | 117 | ||
| 107 | class AssignmentTwo(Assignment): | 118 | class AssignmentTwo(Assignment): |
diff --git a/day9/test_init.py b/day9/test_init.py new file mode 100644 index 0000000..b36eed6 --- /dev/null +++ b/day9/test_init.py | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import os.path | ||
| 3 | |||
| 4 | import day9 | ||
| 5 | |||
| 6 | |||
| 7 | def test_output_visualization(): | ||
| 8 | expected = "..##..\n" "...##.\n" ".####.\n" "....#.\n" "s###.." | ||
| 9 | |||
| 10 | assignment = day9.AssignmentOne(path=os.path.dirname(day9.__file__)) | ||
| 11 | unique_positions = assignment.unique_tail_positions( | ||
| 12 | input=assignment.read_input(True) | ||
| 13 | ) | ||
| 14 | assert expected == assignment.visualize(6, 5, unique_positions) | ||
