From ac12e70b892f34050aba042b27be5b179e0d5e5b Mon Sep 17 00:00:00 2001 From: jkerney Date: Wed, 21 Dec 2022 21:39:31 +1100 Subject: [PATCH 1/4] setup day 15 --- day_15/day_15_part_1.py | 23 +++++++++++++++++++++++ day_15/input.txt | 32 ++++++++++++++++++++++++++++++++ day_15/test.txt | 14 ++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 day_15/day_15_part_1.py create mode 100644 day_15/input.txt create mode 100644 day_15/test.txt diff --git a/day_15/day_15_part_1.py b/day_15/day_15_part_1.py new file mode 100644 index 0000000..ba14a53 --- /dev/null +++ b/day_15/day_15_part_1.py @@ -0,0 +1,23 @@ +""" +Advent of Code 2022 - Day 15 Part 1 +https://adventofcode.com/2022/day/15 +""" + +#from ast import literal_eval +#import numpy +import itertools + +FILE_PATH = "day_15/input.txt" +#FILE_PATH = "day_15/test.txt" + +def main(): + ''' + Day 15 Part 1 + ''' + with open(FILE_PATH, "r", encoding="utf-8") as input_file: + + text_data = input_file.read() + text_data = text_data.splitlines() + +if __name__ == "__main__": + main() diff --git a/day_15/input.txt b/day_15/input.txt new file mode 100644 index 0000000..fe5ff9c --- /dev/null +++ b/day_15/input.txt @@ -0,0 +1,32 @@ +Sensor at x=2832148, y=322979: closest beacon is at x=3015667, y=-141020 +Sensor at x=1449180, y=3883502: closest beacon is at x=2656952, y=4188971 +Sensor at x=2808169, y=1194666: closest beacon is at x=3015667, y=-141020 +Sensor at x=1863363, y=2435968: closest beacon is at x=2166084, y=2883057 +Sensor at x=3558230, y=2190936: closest beacon is at x=3244164, y=2592191 +Sensor at x=711491, y=2444705: closest beacon is at x=617239, y=2988377 +Sensor at x=2727148, y=2766272: closest beacon is at x=2166084, y=2883057 +Sensor at x=2857938, y=3988086: closest beacon is at x=2968511, y=4098658 +Sensor at x=1242410, y=2270153: closest beacon is at x=214592, y=2000000 +Sensor at x=3171784, y=2523127: closest beacon is at x=3244164, y=2592191 +Sensor at x=2293378, y=71434: closest beacon is at x=3015667, y=-141020 +Sensor at x=399711, y=73420: closest beacon is at x=1152251, y=-158441 +Sensor at x=3677529, y=415283: closest beacon is at x=3015667, y=-141020 +Sensor at x=207809, y=2348497: closest beacon is at x=214592, y=2000000 +Sensor at x=60607, y=3403420: closest beacon is at x=617239, y=2988377 +Sensor at x=3767729, y=3136725: closest beacon is at x=4171278, y=3348370 +Sensor at x=3899632, y=3998969: closest beacon is at x=4171278, y=3348370 +Sensor at x=394783, y=1541278: closest beacon is at x=214592, y=2000000 +Sensor at x=1193642, y=642631: closest beacon is at x=1152251, y=-158441 +Sensor at x=122867, y=2661904: closest beacon is at x=214592, y=2000000 +Sensor at x=551012, y=3787568: closest beacon is at x=617239, y=2988377 +Sensor at x=3175715, y=2975144: closest beacon is at x=3244164, y=2592191 +Sensor at x=402217, y=2812449: closest beacon is at x=617239, y=2988377 +Sensor at x=879648, y=1177329: closest beacon is at x=214592, y=2000000 +Sensor at x=1317218, y=2978309: closest beacon is at x=617239, y=2988377 +Sensor at x=3965126, y=1743931: closest beacon is at x=3244164, y=2592191 +Sensor at x=2304348, y=3140055: closest beacon is at x=2166084, y=2883057 +Sensor at x=3380135, y=3650709: closest beacon is at x=2968511, y=4098658 +Sensor at x=49224, y=1914296: closest beacon is at x=214592, y=2000000 +Sensor at x=3096228, y=2457233: closest beacon is at x=3244164, y=2592191 +Sensor at x=1415660, y=6715: closest beacon is at x=1152251, y=-158441 +Sensor at x=2616280, y=3548378: closest beacon is at x=2656952, y=4188971 diff --git a/day_15/test.txt b/day_15/test.txt new file mode 100644 index 0000000..652e631 --- /dev/null +++ b/day_15/test.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 From 3e0781adea795fcbebbc93193c1861df8239ea31 Mon Sep 17 00:00:00 2001 From: Justin Kerney Date: Wed, 21 Dec 2022 22:08:17 +1100 Subject: [PATCH 2/4] day 15 wip --- day_15/day_15_part_1.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/day_15/day_15_part_1.py b/day_15/day_15_part_1.py index ba14a53..eec0a75 100644 --- a/day_15/day_15_part_1.py +++ b/day_15/day_15_part_1.py @@ -1,6 +1,13 @@ """ Advent of Code 2022 - Day 15 Part 1 https://adventofcode.com/2022/day/15 + +- For each sensor, we know the closest beacon +- + +- For a given row, find out how many spots don't have a beacon + + """ #from ast import literal_eval From 6636d9153ee55672196063facedbb5f9adac3354 Mon Sep 17 00:00:00 2001 From: Justin Kerney Date: Sat, 24 Dec 2022 21:38:32 +1100 Subject: [PATCH 3/4] day 15 part 1 complete --- day_15/day_15_part_1.py | 46 ++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/day_15/day_15_part_1.py b/day_15/day_15_part_1.py index eec0a75..a4abc92 100644 --- a/day_15/day_15_part_1.py +++ b/day_15/day_15_part_1.py @@ -1,18 +1,12 @@ """ Advent of Code 2022 - Day 15 Part 1 https://adventofcode.com/2022/day/15 - -- For each sensor, we know the closest beacon -- - -- For a given row, find out how many spots don't have a beacon - - """ #from ast import literal_eval #import numpy -import itertools +#import itertools +import re FILE_PATH = "day_15/input.txt" #FILE_PATH = "day_15/test.txt" @@ -21,10 +15,46 @@ def main(): ''' Day 15 Part 1 ''' + + # Check how many positions on this row we know don't have a beacon + y_test = 2000000 + with open(FILE_PATH, "r", encoding="utf-8") as input_file: text_data = input_file.read() text_data = text_data.splitlines() + text_data = [re.findall(r"x=(-?\d+), y=(-?\d+)", line) for line in text_data] + text_data = [[(int(a), int(b)) for (a,b) in line] for line in text_data] + + no_beacon = [] + dist_beacon = [] + + for sensor, beacon in text_data: + + # Distance from sensor to closest beacon + dist = abs(sensor[0] - beacon[0]) + abs(sensor[1] - beacon[1]) + + # Now check distance to line y_test + dist_y_test = abs(sensor[1] - y_test) + + # Check if any point on y_test line is closer than the beacon + if dist_y_test <= dist: + + no_beacon.append((sensor[0], y_test)) + + for i in range(1, dist - dist_y_test + 1): + no_beacon.append((sensor[0] - i, y_test)) + no_beacon.append((sensor[0] + i, y_test)) + + no_beacon = set(no_beacon) + # Remove beacons from the set + for sensor, beacon in text_data: + try: + no_beacon.remove(beacon) + except: + pass + + print(len(set(no_beacon))) if __name__ == "__main__": main() From 9b8860a83f81c724ab422ab7275513acf61773ef Mon Sep 17 00:00:00 2001 From: Justin Kerney Date: Sun, 25 Dec 2022 11:49:16 +1100 Subject: [PATCH 4/4] day 15 part 2 - non-optimum solution --- day_15/day_15_part_2.py | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 day_15/day_15_part_2.py diff --git a/day_15/day_15_part_2.py b/day_15/day_15_part_2.py new file mode 100644 index 0000000..5b0db8a --- /dev/null +++ b/day_15/day_15_part_2.py @@ -0,0 +1,91 @@ +""" +Advent of Code 2022 - Day 15 Part 2 +https://adventofcode.com/2022/day/15 + +This is very slow. Non-optimum solution for now. +Maybe takes an hour? Committing for now until +I can find a better solution. + +""" + +#from ast import literal_eval +#import numpy +#import itertools +import re + +FILE_PATH = "day_15/input.txt" +#FILE_PATH = "day_15/test.txt" +MAX_COORD = 4000000 +COUNTER_ITR = 100000 + +def add_point(points_check, x, y): + ''' + Check if point is within bounds and add to set + ''' + if x < MAX_COORD and y < MAX_COORD and x >= 0 and y >= 0: + points_check.add((x,y)) + +def main(): + ''' + Day 15 Part 2 + ''' + + with open(FILE_PATH, "r", encoding="utf-8") as input_file: + + text_data = input_file.read() + text_data = text_data.splitlines() + text_data = [re.findall(r"x=(-?\d+), y=(-?\d+)", line) for line in text_data] + data = [[(int(a), int(b)) for (a,b) in line] for line in text_data] + data_with_dist = [] + + points_check = set() + + print("Adding points to check for each sensor") + for sensor, beacon in data: + # Distance from sensor to closest beacon + dist = abs(sensor[0] - beacon[0]) + abs(sensor[1] - beacon[1]) + data_with_dist.append((sensor, beacon, dist)) + print("Sensor location", sensor) + + # Now iterate over every point that is dist+1 away + for i in range(dist+1): + + x = sensor[0] + i + y = sensor[1] + (dist + 1 - i) + add_point(points_check, x, y) + + x = sensor[0] + i + y = sensor[1] - (dist + 1 - i) + add_point(points_check, x, y) + + if i > 0: + x = sensor[0] - i + y = sensor[1] + (dist + 1 - i) + add_point(points_check, x, y) + + x = sensor[0] - i + y = sensor[1] - (dist + 1 - i) + add_point(points_check, x, y) + + print("Completed adding points to check") + print("Number points to check:", len(points_check)) + + counter = 0 + # now for these points, check if they're within distance of any other sensor + for point in points_check: + counter += 1 + if counter % COUNTER_ITR == 0: + print("Completed", counter, "points") + emergency_beacon = True + for sensor, beacon, dist in data_with_dist: + if abs(sensor[0] - point[0]) + abs(sensor[1] - point[1]) <= dist: + emergency_beacon = False + break + + if emergency_beacon == True: + print("Location of emergency beacon:", point) + print("Tuning frequency", point[0] * 4000000 + point[1]) + break + +if __name__ == "__main__": + main()