diff options
Diffstat (limited to 'day15')
| -rw-r--r-- | day15/__init__.py | 60 |
1 files changed, 44 insertions, 16 deletions
diff --git a/day15/__init__.py b/day15/__init__.py index 3ba49d3..b43cae1 100644 --- a/day15/__init__.py +++ b/day15/__init__.py | |||
| @@ -18,21 +18,35 @@ class Sensor: | |||
| 18 | coordinate: Coordinate | 18 | coordinate: Coordinate |
| 19 | nearest: Coordinate | 19 | nearest: Coordinate |
| 20 | 20 | ||
| 21 | def __hash__(self): | ||
| 22 | return hash(self.coordinate) | ||
| 23 | |||
| 21 | def __post_init__(self): | 24 | def __post_init__(self): |
| 22 | self.radius = self.coordinate.distance(self.nearest) | 25 | self.radius = self.coordinate.distance(self.nearest) |
| 23 | 26 | ||
| 24 | 27 | ||
| 25 | @dataclass | 28 | @dataclass |
| 26 | class Map: | 29 | class Map: |
| 27 | sensors: Set[Coordinate] = field(default_factory=set) | 30 | sensors: Set[Sensor] = field(default_factory=set) |
| 28 | beacons: Set[Coordinate] = field(default_factory=set) | 31 | beacons: Set[Coordinate] = field(default_factory=set) |
| 29 | 32 | ||
| 33 | def __post_init__(self): | ||
| 34 | all_coordinates = self.beacons | {sensor.coordinate for sensor in self.sensors} | ||
| 35 | |||
| 36 | min_x = min(c.x for c in all_coordinates) | ||
| 37 | max_x = max(c.x for c in all_coordinates) | ||
| 38 | min_y = min(c.y for c in all_coordinates) | ||
| 39 | max_y = max(c.y for c in all_coordinates) | ||
| 40 | |||
| 41 | self.width = list(range(min_x, max_x + 1)) | ||
| 42 | self.height = list(range(min_y, max_y + 1)) | ||
| 43 | |||
| 30 | 44 | ||
| 31 | input_pattern = re.compile("x=(-?[-0-9]+), y=(-?[0-9]+)") | 45 | input_pattern = re.compile("x=(-?[-0-9]+), y=(-?[0-9]+)") |
| 32 | 46 | ||
| 33 | 47 | ||
| 34 | class Assignment(BaseAssignment, ABC): | 48 | class Assignment(BaseAssignment[int, Tuple[Sensor, Coordinate]], ABC): |
| 35 | def get_coordinates(self, line: str) -> Tuple[Coordinate, Coordinate]: | 49 | def parse_item(self, line: str) -> Tuple[Sensor, Coordinate]: |
| 36 | match = input_pattern.findall(line) | 50 | match = input_pattern.findall(line) |
| 37 | 51 | ||
| 38 | if len(match) != 2: | 52 | if len(match) != 2: |
| @@ -43,27 +57,41 @@ class Assignment(BaseAssignment, ABC): | |||
| 43 | beacon = Coordinate(int(beacon_match[0]), int(beacon_match[1])) | 57 | beacon = Coordinate(int(beacon_match[0]), int(beacon_match[1])) |
| 44 | sensor = Sensor(Coordinate(int(sensor_match[0]), int(sensor_match[1])), beacon) | 58 | sensor = Sensor(Coordinate(int(sensor_match[0]), int(sensor_match[1])), beacon) |
| 45 | 59 | ||
| 46 | result = tuple(Coordinate(int(x), int(y)) for x, y in match) | 60 | return sensor, beacon |
| 47 | 61 | ||
| 48 | return result | 62 | def create_map(self, input: Iterator[Tuple[Sensor, Coordinate]]) -> Map: |
| 63 | sensors = set() | ||
| 64 | beacons = set() | ||
| 49 | 65 | ||
| 50 | def parse_input(self): | 66 | for sensor, beacon in input: |
| 51 | pass | 67 | sensors.add(sensor) |
| 68 | beacons.add(beacon) | ||
| 69 | |||
| 70 | return Map(sensors, beacons) | ||
| 52 | 71 | ||
| 53 | 72 | ||
| 54 | class AssignmentOne(Assignment): | 73 | class AssignmentOne(Assignment): |
| 55 | example_result = 10 | 74 | example_result = 26 |
| 75 | example_kwargs = {"y": 10} | ||
| 56 | 76 | ||
| 57 | def run(self, input: Iterator) -> Any: | 77 | def run(self, input: Iterator[Tuple[Sensor, Coordinate]], y=2000000): |
| 58 | sensors = set() | 78 | map = self.create_map(input) |
| 59 | beacons = set() | ||
| 60 | 79 | ||
| 61 | for line in input: | 80 | no_sensor = 0 |
| 62 | sensor, beacon = self.get_coordinates(line) | 81 | |
| 63 | sensors.add(sensor) | 82 | for x in map.width: |
| 64 | beacons.add(beacon) | 83 | coordinate = Coordinate(x, y) |
| 84 | |||
| 85 | for sensor in map.sensors: | ||
| 86 | if sensor.nearest == coordinate: | ||
| 87 | break | ||
| 88 | |||
| 89 | distance = sensor.coordinate.distance(coordinate) | ||
| 90 | if distance <= sensor.radius: | ||
| 91 | no_sensor += 1 | ||
| 92 | break | ||
| 65 | 93 | ||
| 66 | pass | 94 | return no_sensor |
| 67 | 95 | ||
| 68 | 96 | ||
| 69 | class AssignmentTwo(Assignment): | 97 | class AssignmentTwo(Assignment): |
