From b37323ac00baa3ac5dcc042eaad95d4dc1eeab0f Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 16 Dec 2022 11:48:40 +0100 Subject: Day 15 [WIP] --- day15/__init__.py | 60 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 16 deletions(-) (limited to 'day15/__init__.py') 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: coordinate: Coordinate nearest: Coordinate + def __hash__(self): + return hash(self.coordinate) + def __post_init__(self): self.radius = self.coordinate.distance(self.nearest) @dataclass class Map: - sensors: Set[Coordinate] = field(default_factory=set) + sensors: Set[Sensor] = field(default_factory=set) beacons: Set[Coordinate] = field(default_factory=set) + def __post_init__(self): + all_coordinates = self.beacons | {sensor.coordinate for sensor in self.sensors} + + min_x = min(c.x for c in all_coordinates) + max_x = max(c.x for c in all_coordinates) + min_y = min(c.y for c in all_coordinates) + max_y = max(c.y for c in all_coordinates) + + self.width = list(range(min_x, max_x + 1)) + self.height = list(range(min_y, max_y + 1)) + input_pattern = re.compile("x=(-?[-0-9]+), y=(-?[0-9]+)") -class Assignment(BaseAssignment, ABC): - def get_coordinates(self, line: str) -> Tuple[Coordinate, Coordinate]: +class Assignment(BaseAssignment[int, Tuple[Sensor, Coordinate]], ABC): + def parse_item(self, line: str) -> Tuple[Sensor, Coordinate]: match = input_pattern.findall(line) if len(match) != 2: @@ -43,27 +57,41 @@ class Assignment(BaseAssignment, ABC): beacon = Coordinate(int(beacon_match[0]), int(beacon_match[1])) sensor = Sensor(Coordinate(int(sensor_match[0]), int(sensor_match[1])), beacon) - result = tuple(Coordinate(int(x), int(y)) for x, y in match) + return sensor, beacon - return result + def create_map(self, input: Iterator[Tuple[Sensor, Coordinate]]) -> Map: + sensors = set() + beacons = set() - def parse_input(self): - pass + for sensor, beacon in input: + sensors.add(sensor) + beacons.add(beacon) + + return Map(sensors, beacons) class AssignmentOne(Assignment): - example_result = 10 + example_result = 26 + example_kwargs = {"y": 10} - def run(self, input: Iterator) -> Any: - sensors = set() - beacons = set() + def run(self, input: Iterator[Tuple[Sensor, Coordinate]], y=2000000): + map = self.create_map(input) - for line in input: - sensor, beacon = self.get_coordinates(line) - sensors.add(sensor) - beacons.add(beacon) + no_sensor = 0 + + for x in map.width: + coordinate = Coordinate(x, y) + + for sensor in map.sensors: + if sensor.nearest == coordinate: + break + + distance = sensor.coordinate.distance(coordinate) + if distance <= sensor.radius: + no_sensor += 1 + break - pass + return no_sensor class AssignmentTwo(Assignment): -- cgit v1.2.3