diff options
| author | 2022-12-16 10:48:22 +0100 | |
|---|---|---|
| committer | 2022-12-16 10:48:22 +0100 | |
| commit | fa75898ed35c732ba4e9d455f16dee48ba6c5ca8 (patch) | |
| tree | 395efdbe3de90162bfa906aabf7d004efa205ac6 /day15/__init__.py | |
| parent | 9d716dcf2b49abc34a7f52d2cbe879b7aba13421 (diff) | |
| download | 2022-fa75898ed35c732ba4e9d455f16dee48ba6c5ca8.tar.gz 2022-fa75898ed35c732ba4e9d455f16dee48ba6c5ca8.tar.bz2 2022-fa75898ed35c732ba4e9d455f16dee48ba6c5ca8.zip | |
Day 15 [WIP]
Diffstat (limited to 'day15/__init__.py')
| -rw-r--r-- | day15/__init__.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/day15/__init__.py b/day15/__init__.py new file mode 100644 index 0000000..3ba49d3 --- /dev/null +++ b/day15/__init__.py | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import re | ||
| 3 | from abc import ABC | ||
| 4 | from collections import namedtuple | ||
| 5 | from dataclasses import dataclass, field | ||
| 6 | from typing import Tuple, Iterator, Any, Set | ||
| 7 | |||
| 8 | from aoc import BaseAssignment | ||
| 9 | |||
| 10 | |||
| 11 | class Coordinate(namedtuple("Coordinate", ["x", "y"])): | ||
| 12 | def distance(self, other: "Coordinate"): | ||
| 13 | return abs(other.x - self.x) + abs(other.y - self.y) | ||
| 14 | |||
| 15 | |||
| 16 | @dataclass | ||
| 17 | class Sensor: | ||
| 18 | coordinate: Coordinate | ||
| 19 | nearest: Coordinate | ||
| 20 | |||
| 21 | def __post_init__(self): | ||
| 22 | self.radius = self.coordinate.distance(self.nearest) | ||
| 23 | |||
| 24 | |||
| 25 | @dataclass | ||
| 26 | class Map: | ||
| 27 | sensors: Set[Coordinate] = field(default_factory=set) | ||
| 28 | beacons: Set[Coordinate] = field(default_factory=set) | ||
| 29 | |||
| 30 | |||
| 31 | input_pattern = re.compile("x=(-?[-0-9]+), y=(-?[0-9]+)") | ||
| 32 | |||
| 33 | |||
| 34 | class Assignment(BaseAssignment, ABC): | ||
| 35 | def get_coordinates(self, line: str) -> Tuple[Coordinate, Coordinate]: | ||
| 36 | match = input_pattern.findall(line) | ||
| 37 | |||
| 38 | if len(match) != 2: | ||
| 39 | raise RuntimeError() | ||
| 40 | |||
| 41 | sensor_match, beacon_match = match | ||
| 42 | |||
| 43 | beacon = Coordinate(int(beacon_match[0]), int(beacon_match[1])) | ||
| 44 | sensor = Sensor(Coordinate(int(sensor_match[0]), int(sensor_match[1])), beacon) | ||
| 45 | |||
| 46 | result = tuple(Coordinate(int(x), int(y)) for x, y in match) | ||
| 47 | |||
| 48 | return result | ||
| 49 | |||
| 50 | def parse_input(self): | ||
| 51 | pass | ||
| 52 | |||
| 53 | |||
| 54 | class AssignmentOne(Assignment): | ||
| 55 | example_result = 10 | ||
| 56 | |||
| 57 | def run(self, input: Iterator) -> Any: | ||
| 58 | sensors = set() | ||
| 59 | beacons = set() | ||
| 60 | |||
| 61 | for line in input: | ||
| 62 | sensor, beacon = self.get_coordinates(line) | ||
| 63 | sensors.add(sensor) | ||
| 64 | beacons.add(beacon) | ||
| 65 | |||
| 66 | pass | ||
| 67 | |||
| 68 | |||
| 69 | class AssignmentTwo(Assignment): | ||
| 70 | pass | ||
