diff --git a/python/day15/bsp.txt b/python/day15/bsp.txt new file mode 100644 index 0000000..652e631 --- /dev/null +++ b/python/day15/bsp.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 \ No newline at end of file diff --git a/python/day15/day15_1.py b/python/day15/day15_1.py new file mode 100644 index 0000000..c6fac7b --- /dev/null +++ b/python/day15/day15_1.py @@ -0,0 +1,37 @@ +lines = open("input.txt", "r").read().splitlines() + + +def parse_lines(lines: list) -> (set, set): + sensors = [] + beacons = [] + for line in lines: + _, _, sx, sy, _, _, _, _, bx, by = line.split(" ") + sensors.append([int(sx.split("=")[1][:-1]), int(sy.split("=")[1][:-1])]) + beacons.append([int(bx.split("=")[1][:-1]), int(by.split("=")[1])]) + return sensors, beacons + + +def manhatten(a, b): + return abs(a[0] - b[0]) + abs(a[1] - b[1]) + + +critical = 2000000 +sensors, beacons = parse_lines(lines) +taken = set() +critical_beacons = set(tuple(x) for x in beacons if x[1] == critical) +print(critical_beacons) +for i in range(len(sensors)): + print(i) + s = sensors[i] + b = beacons[i] + d = manhatten(s, b) + min_y, max_y = s[1] - d, s[1] + d + if min_y <= critical <= max_y: + critical_d = abs(s[1] - critical) + critical_reach = d - critical_d + taken.update( + (x, critical) + for x in range(s[0] - critical_reach, s[0] + critical_reach + 1) + ) + +print(len(taken) - len(critical_beacons)) diff --git a/python/day15/day15_2.py b/python/day15/day15_2.py new file mode 100644 index 0000000..f3c53ce --- /dev/null +++ b/python/day15/day15_2.py @@ -0,0 +1,68 @@ +lines = open("input.txt", "r").read().splitlines() + + +def parse_lines(lines: list) -> (set, set): + sensors = [] + beacons = [] + for line in lines: + _, _, sx, sy, _, _, _, _, bx, by = line.split(" ") + sensors.append((int(sx.split("=")[1][:-1]), int(sy.split("=")[1][:-1]))) + beacons.append((int(bx.split("=")[1][:-1]), int(by.split("=")[1]))) + return sensors, beacons + + +def manhatten(a, b): + return abs(a[0] - b[0]) + abs(a[1] - b[1]) + + +def build_search_space(sensors, beacons): + space = set() + for sensor, beacon in zip(sensors, beacons): + space.update(sensor_diamond(sensor, beacon)) + return space + + +def sensor_diamond(sensor, beacon): + points = set() + d = manhatten(sensor, beacon) + xr = range(d + 2) + yr = xr[::-1] + xrneg = range(0, (d + 2) * -1, -1) + yrneg = xrneg[::-1] + for x, y, xneg, yneg in zip(xr, yr, xrneg, yrneg): + points.add((sensor[0] + x, sensor[1] + y)) + points.add((sensor[0] + xneg, sensor[1] + y)) + points.add((sensor[0] + x, sensor[1] + yneg)) + points.add((sensor[0] + xneg, sensor[1] + yneg)) + return points + + +def search_candidates(sensors, beacons, space): + candidates = set() + counter = 0 + for point in space: + is_candidate = False + if x_min <= point[0] <= x_max and y_min <= point[1] <= y_max: + is_candidate = True + for sensor, beacon in zip(sensors, beacons): + if manhatten(sensor, point) <= manhatten(sensor, beacon): + is_candidate = False + if is_candidate: + print("candidate found") + candidates.add(point) + counter += 1 + if counter % 100000 == 0: + print(f"{counter} points searched") + return candidates + + +x_min, x_max = 0, 4000000 +y_min, y_max = 0, 4000000 +sensors, beacons = parse_lines(lines) + +space = build_search_space(sensors, beacons) +print("space set up") +candidates = search_candidates(sensors, beacons, space) +print(candidates) +x, y = candidates.pop() +print(x * 4000000 + y) diff --git a/python/day15/input.txt b/python/day15/input.txt new file mode 100644 index 0000000..e6a6df5 --- /dev/null +++ b/python/day15/input.txt @@ -0,0 +1,33 @@ +Sensor at x=2302110, y=2237242: closest beacon is at x=2348729, y=1239977 +Sensor at x=47903, y=2473047: closest beacon is at x=-432198, y=2000000 +Sensor at x=2363579, y=1547888: closest beacon is at x=2348729, y=1239977 +Sensor at x=3619841, y=520506: closest beacon is at x=2348729, y=1239977 +Sensor at x=3941908, y=3526118: closest beacon is at x=3772294, y=3485243 +Sensor at x=3206, y=1564595: closest beacon is at x=-432198, y=2000000 +Sensor at x=3123411, y=3392077: closest beacon is at x=2977835, y=3592946 +Sensor at x=3279053, y=3984688: closest beacon is at x=2977835, y=3592946 +Sensor at x=2968162, y=3938490: closest beacon is at x=2977835, y=3592946 +Sensor at x=1772120, y=2862246: closest beacon is at x=2017966, y=3158243 +Sensor at x=3283241, y=2619168: closest beacon is at x=3172577, y=2521434 +Sensor at x=2471642, y=3890150: closest beacon is at x=2977835, y=3592946 +Sensor at x=3163348, y=3743489: closest beacon is at x=2977835, y=3592946 +Sensor at x=2933313, y=2919047: closest beacon is at x=3172577, y=2521434 +Sensor at x=2780640, y=3629927: closest beacon is at x=2977835, y=3592946 +Sensor at x=3986978, y=2079918: closest beacon is at x=3998497, y=2812428 +Sensor at x=315464, y=370694: closest beacon is at x=-550536, y=260566 +Sensor at x=3957316, y=3968366: closest beacon is at x=3772294, y=3485243 +Sensor at x=2118533, y=1074658: closest beacon is at x=2348729, y=1239977 +Sensor at x=3494855, y=3378533: closest beacon is at x=3772294, y=3485243 +Sensor at x=2575727, y=210553: closest beacon is at x=2348729, y=1239977 +Sensor at x=3999990, y=2813525: closest beacon is at x=3998497, y=2812428 +Sensor at x=3658837, y=3026912: closest beacon is at x=3998497, y=2812428 +Sensor at x=1551619, y=1701155: closest beacon is at x=2348729, y=1239977 +Sensor at x=2625855, y=3330422: closest beacon is at x=2977835, y=3592946 +Sensor at x=3476946, y=2445098: closest beacon is at x=3172577, y=2521434 +Sensor at x=2915568, y=1714113: closest beacon is at x=2348729, y=1239977 +Sensor at x=729668, y=3723377: closest beacon is at x=-997494, y=3617758 +Sensor at x=3631681, y=3801747: closest beacon is at x=3772294, y=3485243 +Sensor at x=2270816, y=3197807: closest beacon is at x=2017966, y=3158243 +Sensor at x=3999999, y=2810929: closest beacon is at x=3998497, y=2812428 +Sensor at x=3978805, y=3296024: closest beacon is at x=3772294, y=3485243 +Sensor at x=1054910, y=811769: closest beacon is at x=2348729, y=1239977