diff options
Diffstat (limited to 'day15/__init__.py')
| -rw-r--r-- | day15/__init__.py | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/day15/__init__.py b/day15/__init__.py index 5885f37..38bef35 100644 --- a/day15/__init__.py +++ b/day15/__init__.py | |||
| @@ -1,17 +1,12 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | import re | 2 | import re |
| 3 | from abc import ABC | 3 | from abc import ABC |
| 4 | from collections import namedtuple | ||
| 5 | from dataclasses import dataclass, field | 4 | from dataclasses import dataclass, field |
| 6 | from typing import Tuple, Iterator, Any, Set, Union | 5 | from typing import Union |
| 7 | from typing import Tuple, Iterator, Any, Set, List | 6 | from typing import Tuple, Iterator, Set, List |
| 8 | 7 | ||
| 9 | from aoc import BaseAssignment | 8 | from aoc import BaseAssignment |
| 10 | 9 | from aoc.datastructures import Coordinate | |
| 11 | |||
| 12 | class Coordinate(namedtuple("Coordinate", ["x", "y"])): | ||
| 13 | def distance(self, other: "Coordinate"): | ||
| 14 | return abs(self.x - other.x) + abs(self.y - other.y) | ||
| 15 | 10 | ||
| 16 | 11 | ||
| 17 | @dataclass | 12 | @dataclass |
| @@ -24,10 +19,10 @@ class Sensor: | |||
| 24 | return hash(self.coordinate) | 19 | return hash(self.coordinate) |
| 25 | 20 | ||
| 26 | def __post_init__(self): | 21 | def __post_init__(self): |
| 27 | self.radius = self.coordinate.distance(self.nearest) | 22 | self.radius = self.coordinate.manhattan_distance(self.nearest) |
| 28 | 23 | ||
| 29 | def within_radius(self, coordinate: Coordinate) -> bool: | 24 | def within_radius(self, coordinate: Coordinate) -> bool: |
| 30 | distance = self.coordinate.distance(coordinate) | 25 | distance = self.coordinate.manhattan_distance(coordinate) |
| 31 | return distance <= self.radius | 26 | return distance <= self.radius |
| 32 | 27 | ||
| 33 | def x_coordinates_within_radius_at(self, y: int, map: "Map") -> Union[range, list]: | 28 | def x_coordinates_within_radius_at(self, y: int, map: "Map") -> Union[range, list]: |
