Compare commits
3 commits
3e42548f95
...
14e8b539c6
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 14e8b539c6 | ||
Bruno BELANYI | 0f91c37d6d | ||
Bruno BELANYI | 1ce063154e |
98
2022/d15/ex1/ex1.py
Executable file
98
2022/d15/ex1/ex1.py
Executable file
|
@ -0,0 +1,98 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import dataclasses
|
||||
import functools
|
||||
import sys
|
||||
from collections.abc import Iterable
|
||||
from typing import NamedTuple, Optional
|
||||
|
||||
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
|
||||
@classmethod
|
||||
def from_input(cls, input: str) -> "Point":
|
||||
assert input.startswith("x=") # Sanity check
|
||||
x, y = input.split(", ")
|
||||
return cls(int(x.split("=")[-1]), int(y.split("=")[-1]))
|
||||
|
||||
|
||||
class Interval(NamedTuple):
|
||||
start: int
|
||||
end: int
|
||||
|
||||
def as_set(self) -> set[int]:
|
||||
return set(range(self.start, self.end + 1))
|
||||
|
||||
|
||||
def merge_intervals(intervals: Iterable[Interval]) -> list[Interval]:
|
||||
intervals = sorted(intervals)
|
||||
|
||||
res = [intervals[0]]
|
||||
for candidate in intervals[1:]:
|
||||
# Range is inclusive in both end, so add 1 to end in case of near miss
|
||||
if (res[-1].end + 1) >= candidate.start:
|
||||
new_end = max(res[-1].end, candidate.end)
|
||||
res[-1] = Interval(res[-1].start, new_end)
|
||||
else:
|
||||
res.append(candidate)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def distance(a: Point, b: Point) -> int:
|
||||
return abs(a.x - b.x) + abs(a.y - b.y)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class SensorData:
|
||||
pos: Point
|
||||
beacon: Point
|
||||
|
||||
@classmethod
|
||||
def from_input(cls, input: str) -> "SensorData":
|
||||
assert input.startswith("Sensor at x=") # Sanity check
|
||||
|
||||
mid = input.index(":")
|
||||
sensor, beacon = input[:mid], input[mid:]
|
||||
return cls(
|
||||
Point.from_input(sensor[sensor.index("x") :]),
|
||||
Point.from_input(beacon[beacon.index("x") :]),
|
||||
)
|
||||
|
||||
@functools.cached_property
|
||||
def safe_range(self) -> int:
|
||||
return distance(self.pos, self.beacon)
|
||||
|
||||
def scan_row(self, row: int) -> Optional[Interval]:
|
||||
distance = abs(row - self.pos.y)
|
||||
dx = self.safe_range - distance
|
||||
if dx < 0:
|
||||
return None
|
||||
return Interval(self.pos.x - dx, self.pos.x + dx)
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def points_without_sos(data: list[SensorData], row: int) -> list[Interval]:
|
||||
intervals = (d.scan_row(row) for d in data)
|
||||
return merge_intervals(i for i in intervals if i is not None)
|
||||
|
||||
data = [SensorData.from_input(line) for line in input]
|
||||
beacons = {d.beacon for d in data}
|
||||
|
||||
ROW = 2_000_000
|
||||
intervals = points_without_sos(data, ROW)
|
||||
return len(
|
||||
set.union(*(i.as_set() for i in intervals))
|
||||
- {b.x for b in beacons if b.y == ROW}
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
112
2022/d15/ex2/ex2.py
Executable file
112
2022/d15/ex2/ex2.py
Executable file
|
@ -0,0 +1,112 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import dataclasses
|
||||
import functools
|
||||
import sys
|
||||
from collections.abc import Iterable
|
||||
from typing import NamedTuple, Optional
|
||||
|
||||
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
|
||||
@classmethod
|
||||
def from_input(cls, input: str) -> "Point":
|
||||
assert input.startswith("x=") # Sanity check
|
||||
x, y = input.split(", ")
|
||||
return cls(int(x.split("=")[-1]), int(y.split("=")[-1]))
|
||||
|
||||
|
||||
class Interval(NamedTuple):
|
||||
start: int
|
||||
end: int
|
||||
|
||||
def as_set(self) -> set[int]:
|
||||
return set(range(self.start, self.end + 1))
|
||||
|
||||
|
||||
def merge_intervals(intervals: Iterable[Interval]) -> list[Interval]:
|
||||
intervals = sorted(intervals)
|
||||
|
||||
res = [intervals[0]]
|
||||
for candidate in intervals[1:]:
|
||||
# Range is inclusive in both end, so add 1 to end in case of near miss
|
||||
if (res[-1].end + 1) >= candidate.start:
|
||||
new_end = max(res[-1].end, candidate.end)
|
||||
res[-1] = Interval(res[-1].start, new_end)
|
||||
else:
|
||||
res.append(candidate)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def distance(a: Point, b: Point) -> int:
|
||||
return abs(a.x - b.x) + abs(a.y - b.y)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class SensorData:
|
||||
pos: Point
|
||||
beacon: Point
|
||||
|
||||
@classmethod
|
||||
def from_input(cls, input: str) -> "SensorData":
|
||||
assert input.startswith("Sensor at x=") # Sanity check
|
||||
|
||||
mid = input.index(":")
|
||||
sensor, beacon = input[:mid], input[mid:]
|
||||
return cls(
|
||||
Point.from_input(sensor[sensor.index("x") :]),
|
||||
Point.from_input(beacon[beacon.index("x") :]),
|
||||
)
|
||||
|
||||
@functools.cached_property
|
||||
def safe_range(self) -> int:
|
||||
return distance(self.pos, self.beacon)
|
||||
|
||||
def scan_row(self, row: int) -> Optional[Interval]:
|
||||
distance = abs(row - self.pos.y)
|
||||
dx = self.safe_range - distance
|
||||
if dx < 0:
|
||||
return None
|
||||
return Interval(self.pos.x - dx, self.pos.x + dx)
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def points_without_sos(data: list[SensorData], row: int) -> list[Interval]:
|
||||
intervals = (d.scan_row(row) for d in data)
|
||||
return merge_intervals(i for i in intervals if i is not None)
|
||||
|
||||
def find_hole(intervals: list[Interval], max_coord: int) -> Optional[int]:
|
||||
for i in intervals:
|
||||
if i.start > 0:
|
||||
return i.start - 1
|
||||
if i.end < max_coord:
|
||||
return i.end + 1
|
||||
|
||||
return None
|
||||
|
||||
def find_sos(data: list[SensorData], max_coord: int) -> Point:
|
||||
for row in range(0, max_coord + 1):
|
||||
intervals = points_without_sos(data, row)
|
||||
if (hole := find_hole(intervals, max_coord)) is not None:
|
||||
return Point(hole, row)
|
||||
assert False # Sanity check
|
||||
|
||||
def tuning_frequency(p: Point) -> int:
|
||||
return p.x * 4000000 + p.y
|
||||
|
||||
data = [SensorData.from_input(line) for line in input]
|
||||
MAX_COORD = 4_000_000
|
||||
sos_beacon = find_sos(data, MAX_COORD)
|
||||
return tuning_frequency(sos_beacon)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
33
2022/d15/ex2/input
Normal file
33
2022/d15/ex2/input
Normal file
|
@ -0,0 +1,33 @@
|
|||
Sensor at x=407069, y=1770807: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=2968955, y=2961853: closest beacon is at x=2700669, y=3091664
|
||||
Sensor at x=3069788, y=2289672: closest beacon is at x=3072064, y=2287523
|
||||
Sensor at x=2206, y=1896380: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=3010408, y=2580417: closest beacon is at x=2966207, y=2275132
|
||||
Sensor at x=2511130, y=2230361: closest beacon is at x=2966207, y=2275132
|
||||
Sensor at x=65435, y=2285654: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=2811709, y=3379959: closest beacon is at x=2801189, y=3200444
|
||||
Sensor at x=168413, y=3989039: closest beacon is at x=-631655, y=3592291
|
||||
Sensor at x=165506, y=2154294: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=2720578, y=3116882: closest beacon is at x=2700669, y=3091664
|
||||
Sensor at x=786521, y=1485720: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=82364, y=2011850: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=2764729, y=3156203: closest beacon is at x=2801189, y=3200444
|
||||
Sensor at x=1795379, y=1766882: closest beacon is at x=1616322, y=907350
|
||||
Sensor at x=2708986, y=3105910: closest beacon is at x=2700669, y=3091664
|
||||
Sensor at x=579597, y=439: closest beacon is at x=1616322, y=907350
|
||||
Sensor at x=2671201, y=2736834: closest beacon is at x=2700669, y=3091664
|
||||
Sensor at x=3901, y=2089464: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=144449, y=813212: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=3619265, y=3169784: closest beacon is at x=2801189, y=3200444
|
||||
Sensor at x=2239333, y=3878605: closest beacon is at x=2801189, y=3200444
|
||||
Sensor at x=2220630, y=2493371: closest beacon is at x=2966207, y=2275132
|
||||
Sensor at x=1148022, y=403837: closest beacon is at x=1616322, y=907350
|
||||
Sensor at x=996105, y=3077490: closest beacon is at x=2700669, y=3091664
|
||||
Sensor at x=3763069, y=3875159: closest beacon is at x=2801189, y=3200444
|
||||
Sensor at x=3994575, y=2268273: closest beacon is at x=3072064, y=2287523
|
||||
Sensor at x=3025257, y=2244500: closest beacon is at x=2966207, y=2275132
|
||||
Sensor at x=2721366, y=1657084: closest beacon is at x=2966207, y=2275132
|
||||
Sensor at x=3783491, y=1332930: closest beacon is at x=3072064, y=2287523
|
||||
Sensor at x=52706, y=2020407: closest beacon is at x=105942, y=2000000
|
||||
Sensor at x=2543090, y=47584: closest beacon is at x=3450858, y=-772833
|
||||
Sensor at x=3499766, y=2477193: closest beacon is at x=3072064, y=2287523
|
Loading…
Reference in a new issue