my solutions for advent of code 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

2 years ago
beacons = []
sensors = []
lineY = 2000000
vectors = []
def loop():
for j in range(len(vectors)):
for i in range(len(vectors)):
if j != i:
if vectors[j][0] in range (vectors[i][0], vectors[i][1]) or vectors[i][0] in range (vectors[j][0], vectors[j][1]):
tempSt = min(vectors[j][0],vectors[i][0])
tempEn = max(vectors[j][1],vectors[i][1])
vectors.append((tempSt, tempEn))
vectors.pop(max(i, j))
vectors.pop(min(i, j))
return False
return True
with open('input15.txt','r') as f:
inp = f.read().splitlines(keepends=False)
for line in inp:
tempA, tempB = line.split(': closest beacon is at x=')
tempA = tempA.replace('Sensor at x=', '')
tempA = tempA.split(', y=')
sensors.append(tuple(map(int, tempA)))
tempB = tempB.split(', y=')
beacons.append(tuple(map(int, tempB)))
for i in range(len(sensors)):
print('checking sensor', i, 'out of', len(sensors) - 1)
(sensX, sensY) = sensors[i]
(beacX, beacY) = beacons[i]
distance = (max(sensX, beacX) - min(sensX, beacX)) + (max(sensY, beacY) - min(sensY, beacY))
if sensY <= lineY and (sensY + distance) >= lineY:
modifier = distance - (lineY - sensY)
vectors.append((sensX - modifier, sensX + modifier))
elif sensY >= lineY and (sensY - distance) <= lineY:
modifier = distance - (sensY - lineY)
vectors.append((sensX - modifier, sensX + modifier))
done = False
tempA = []
while not done:
done = loop()
vectors = sorted(vectors)
CoveredPoints = vectors[-1][1] - vectors[0][0] + 1
for i in range(len(vectors) - 1):
if vectors[i + 1][0] != vectors[i][1]:
CoveredPoints -= (vectors[i + 1][0] - vectors[i][1] - 1)
for beacon in beacons:
if beacon[1] == lineY and beacon not in tempA:
tempA.append(beacon)
CoveredPoints -= 1
print(CoveredPoints)