From fa75898ed35c732ba4e9d455f16dee48ba6c5ca8 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 16 Dec 2022 10:48:22 +0100 Subject: Day 15 [WIP] --- day15/__init__.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ day15/example.txt | 14 +++++++++++ day15/input.txt | 40 +++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 day15/__init__.py create mode 100644 day15/example.txt create mode 100644 day15/input.txt (limited to 'day15') 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 @@ +# -*- coding: utf-8 -*- +import re +from abc import ABC +from collections import namedtuple +from dataclasses import dataclass, field +from typing import Tuple, Iterator, Any, Set + +from aoc import BaseAssignment + + +class Coordinate(namedtuple("Coordinate", ["x", "y"])): + def distance(self, other: "Coordinate"): + return abs(other.x - self.x) + abs(other.y - self.y) + + +@dataclass +class Sensor: + coordinate: Coordinate + nearest: Coordinate + + def __post_init__(self): + self.radius = self.coordinate.distance(self.nearest) + + +@dataclass +class Map: + sensors: Set[Coordinate] = field(default_factory=set) + beacons: Set[Coordinate] = field(default_factory=set) + + +input_pattern = re.compile("x=(-?[-0-9]+), y=(-?[0-9]+)") + + +class Assignment(BaseAssignment, ABC): + def get_coordinates(self, line: str) -> Tuple[Coordinate, Coordinate]: + match = input_pattern.findall(line) + + if len(match) != 2: + raise RuntimeError() + + sensor_match, beacon_match = match + + 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 result + + def parse_input(self): + pass + + +class AssignmentOne(Assignment): + example_result = 10 + + def run(self, input: Iterator) -> Any: + sensors = set() + beacons = set() + + for line in input: + sensor, beacon = self.get_coordinates(line) + sensors.add(sensor) + beacons.add(beacon) + + pass + + +class AssignmentTwo(Assignment): + pass diff --git a/day15/example.txt b/day15/example.txt new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/day15/example.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..e663ca5 --- /dev/null +++ b/day15/input.txt @@ -0,0 +1,40 @@ +Sensor at x=1112863, y=496787: closest beacon is at x=1020600, y=2000000 +Sensor at x=2980210, y=1712427: closest beacon is at x=2946825, y=1712605 +Sensor at x=2799204, y=1425283: closest beacon is at x=2946825, y=1712605 +Sensor at x=3999908, y=2754283: closest beacon is at x=4064129, y=2651511 +Sensor at x=760990, y=1455625: closest beacon is at x=1020600, y=2000000 +Sensor at x=3996490, y=3239979: closest beacon is at x=4064129, y=2651511 +Sensor at x=3347352, y=3603589: closest beacon is at x=3621840, y=3614596 +Sensor at x=2888433, y=2337157: closest beacon is at x=2946825, y=1712605 +Sensor at x=3423261, y=2191958: closest beacon is at x=3153728, y=1862250 +Sensor at x=1160237, y=3999960: closest beacon is at x=109153, y=3585462 +Sensor at x=693519, y=3701289: closest beacon is at x=109153, y=3585462 +Sensor at x=2615270, y=2824808: closest beacon is at x=2554122, y=2935074 +Sensor at x=3046971, y=1755494: closest beacon is at x=2946825, y=1712605 +Sensor at x=139591, y=1186912: closest beacon is at x=1020600, y=2000000 +Sensor at x=2309134, y=47090: closest beacon is at x=3211831, y=-792661 +Sensor at x=1849154, y=1377259: closest beacon is at x=2946825, y=1712605 +Sensor at x=2515971, y=2851853: closest beacon is at x=2554122, y=2935074 +Sensor at x=2524614, y=2738138: closest beacon is at x=2554122, y=2935074 +Sensor at x=3811778, y=1370280: closest beacon is at x=3153728, y=1862250 +Sensor at x=2615590, y=3819371: closest beacon is at x=2554122, y=2935074 +Sensor at x=3996286, y=3719213: closest beacon is at x=3621840, y=3614596 +Sensor at x=3963152, y=2368927: closest beacon is at x=4064129, y=2651511 +Sensor at x=3495504, y=3076982: closest beacon is at x=3621840, y=3614596 +Sensor at x=3725521, y=2560764: closest beacon is at x=4064129, y=2651511 +Sensor at x=952643, y=2385401: closest beacon is at x=1020600, y=2000000 +Sensor at x=3934384, y=2596106: closest beacon is at x=4064129, y=2651511 +Sensor at x=3060628, y=3082730: closest beacon is at x=2554122, y=2935074 +Sensor at x=3468382, y=3916817: closest beacon is at x=3621840, y=3614596 +Sensor at x=3300107, y=469364: closest beacon is at x=3211831, y=-792661 +Sensor at x=2306388, y=1932261: closest beacon is at x=2946825, y=1712605 +Sensor at x=1965, y=3514070: closest beacon is at x=109153, y=3585462 +Sensor at x=3081537, y=1841861: closest beacon is at x=3153728, y=1862250 +Sensor at x=2997643, y=1729779: closest beacon is at x=2946825, y=1712605 +Sensor at x=21714, y=3624181: closest beacon is at x=109153, y=3585462 +Sensor at x=1549467, y=3109269: closest beacon is at x=2554122, y=2935074 +Sensor at x=3722307, y=3839410: closest beacon is at x=3621840, y=3614596 +Sensor at x=3848580, y=3544878: closest beacon is at x=3621840, y=3614596 +Sensor at x=1189516, y=2153239: closest beacon is at x=1020600, y=2000000 +Sensor at x=468190, y=1889204: closest beacon is at x=1020600, y=2000000 +Sensor at x=270403, y=2762568: closest beacon is at x=109153, y=3585462 -- cgit v1.2.3