1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
|