Skip to content

Commit

Permalink
15th December
Browse files Browse the repository at this point in the history
  • Loading branch information
lismith2-cisco committed Dec 28, 2022
1 parent db52738 commit 7c82c87
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 2022_15_beacons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import re
import math

line_re = re.compile(
r"Sensor at x=(?P<sensor_x>[\d-]+), y=(?P<sensor_y>[\d-]+): closest beacon is at x=(?P<beacon_x>[\d-]+), y=(?P<beacon_y>[\d-]+)")


global_min_x = 0
global_max_x = 4000000
global_min_y = 0
global_max_y = 4000000


class Sensor:
def __init__(self, sensor_pos, beacon_pos):
self.sensor_pos = sensor_pos
self.beacon_distance = abs(
sensor_pos[0] - beacon_pos[0]) + abs(sensor_pos[1] - beacon_pos[1])

def get_x_range(self, y):
max_x_distance = self.beacon_distance - abs(y-self.sensor_pos[1])
min_x = self.sensor_pos[0] - max_x_distance
max_x = self.sensor_pos[0] + max_x_distance
return min_x, max_x


all_sensors = []

for line in open("2022_15_beacons_input.txt"):
match = re.match(line_re, line)
sensor_pos = (int(match.group('sensor_x')), int(match.group('sensor_y')))
beacon_pos = (int(match.group('beacon_x')), int(match.group('beacon_y')))
sensor = Sensor(sensor_pos, beacon_pos)
all_sensors.append(sensor)

for y in range(global_min_y, global_max_y + 1):
ranges = sorted(sensor.get_x_range(y) for sensor in all_sensors)
x = global_min_x
for min_x, max_x in ranges:
if min_x <= x <= max_x:
x = max_x + 1
if x <= global_max_x:
break

location = (x, y)

print(location)
tuning_frequency = location[0] * 4000000 + location[1]
print(tuning_frequency)
29 changes: 29 additions & 0 deletions 2022_15_beacons_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Sensor at x=2389280, y=2368338: closest beacon is at x=2127703, y=2732666
Sensor at x=1882900, y=3151610: closest beacon is at x=2127703, y=2732666
Sensor at x=2480353, y=3555879: closest beacon is at x=2092670, y=3609041
Sensor at x=93539, y=965767: closest beacon is at x=501559, y=361502
Sensor at x=357769, y=2291291: closest beacon is at x=262473, y=2000000
Sensor at x=2237908, y=1893142: closest beacon is at x=2127703, y=2732666
Sensor at x=2331355, y=3906306: closest beacon is at x=2092670, y=3609041
Sensor at x=3919787, y=2021847: closest beacon is at x=2795763, y=2589706
Sensor at x=3501238, y=3327244: closest beacon is at x=3562181, y=3408594
Sensor at x=1695968, y=2581703: closest beacon is at x=2127703, y=2732666
Sensor at x=3545913, y=3356504: closest beacon is at x=3562181, y=3408594
Sensor at x=1182450, y=1405295: closest beacon is at x=262473, y=2000000
Sensor at x=3067566, y=3753120: closest beacon is at x=3562181, y=3408594
Sensor at x=1835569, y=3983183: closest beacon is at x=2092670, y=3609041
Sensor at x=127716, y=2464105: closest beacon is at x=262473, y=2000000
Sensor at x=3065608, y=3010074: closest beacon is at x=2795763, y=2589706
Sensor at x=2690430, y=2693094: closest beacon is at x=2795763, y=2589706
Sensor at x=2051508, y=3785175: closest beacon is at x=2092670, y=3609041
Sensor at x=2377394, y=3043562: closest beacon is at x=2127703, y=2732666
Sensor at x=1377653, y=37024: closest beacon is at x=501559, y=361502
Sensor at x=2758174, y=2627042: closest beacon is at x=2795763, y=2589706
Sensor at x=1968468, y=2665146: closest beacon is at x=2127703, y=2732666
Sensor at x=3993311, y=3779031: closest beacon is at x=3562181, y=3408594
Sensor at x=159792, y=1923149: closest beacon is at x=262473, y=2000000
Sensor at x=724679, y=3489022: closest beacon is at x=2092670, y=3609041
Sensor at x=720259, y=121267: closest beacon is at x=501559, y=361502
Sensor at x=6, y=46894: closest beacon is at x=501559, y=361502
Sensor at x=21501, y=2098549: closest beacon is at x=262473, y=2000000
Sensor at x=2974083, y=551886: closest beacon is at x=4271266, y=-98555

0 comments on commit 7c82c87

Please sign in to comment.