day15 a bit simpler

This commit is contained in:
Ruediger Ludwig 2023-01-16 20:29:59 +01:00
parent 3d00f265ca
commit d02d8190f0
4 changed files with 11 additions and 14 deletions

View file

@ -1,4 +1,4 @@
10/20
10
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

View file

@ -1,4 +1,4 @@
2000000/4000000
2000000
Sensor at x=3859432, y=2304903: closest beacon is at x=3677247, y=3140958
Sensor at x=2488890, y=2695345: closest beacon is at x=1934788, y=2667279
Sensor at x=3901948, y=701878: closest beacon is at x=4095477, y=368031

View file

@ -4,21 +4,21 @@ from itertools import combinations
from typing import Iterator, Self
from advent.common.position import ORIGIN, Position
from advent.common.position import Position
day_num = 15
def part1(lines: Iterator[str]) -> int:
row, _ = next(lines).split('/')
row = int(next(lines))
sensor_map = SensorMap.parse(lines)
return sensor_map.count_impossible(int(row))
return sensor_map.count_impossible(row)
def part2(lines: Iterator[str]) -> int:
_, max_range = next(lines).split('/')
next(lines)
sensor_map = SensorMap.parse(lines)
return sensor_map.get_possible_frequency(int(max_range))
return sensor_map.get_possible_frequency()
ColRange = tuple[int, int]
@ -173,8 +173,7 @@ class SensorMap:
Position(sensor2.position.x - sensor2.distance - 1,
sensor2.position.y))
def get_possible_frequency(self, max_range: int) -> int:
max_point = Position.splat(max_range)
def get_possible_frequency(self) -> int:
midlines: list[ManhattenLine] = []
for sensor1, sensor2 in combinations(self.sensors, 2):
midline = SensorMap.get_midline(sensor1, sensor2)
@ -182,9 +181,7 @@ class SensorMap:
midlines.append(midline)
for line1, line2 in combinations(midlines, 2):
point = line1.crosspoint(line2)
if point is not None:
if (point.is_within(ORIGIN, max_point)
and all(not sensor.is_within(point) for sensor in self.sensors)):
if point is not None and all(not sensor.is_within(point) for sensor in self.sensors):
return SensorMap.tuning_frequency(point)
raise Exception("No point found")

View file

@ -46,5 +46,5 @@ def test_possible():
next(lines)
sensor_map = SensorMap.parse(lines)
expected = 56000011
result = sensor_map.get_possible_frequency(20)
result = sensor_map.get_possible_frequency()
assert result == expected