Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions day_15/day_15_part_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Advent of Code 2022 - Day 15 Part 1
https://adventofcode.com/2022/day/15
"""

#from ast import literal_eval
#import numpy
#import itertools
import re

FILE_PATH = "day_15/input.txt"
#FILE_PATH = "day_15/test.txt"

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()
91 changes: 91 additions & 0 deletions day_15/day_15_part_2.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions day_15/input.txt
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions day_15/test.txt
Original file line number Diff line number Diff line change
@@ -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