diff --git a/day15-1.py b/day15-1.py new file mode 100644 index 0000000..e1890a9 --- /dev/null +++ b/day15-1.py @@ -0,0 +1,61 @@ +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) \ No newline at end of file diff --git a/day15-2.py b/day15-2.py new file mode 100644 index 0000000..21c0f3d --- /dev/null +++ b/day15-2.py @@ -0,0 +1,79 @@ +beacons = [] +sensors = [] +CoveredPoints = {} +maxcoord = 4000000 + +def loop(vectors): + 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, vectors + return True, vectors + +def loop2(vectors): + if len(vectors) > 2: + for i in range(len(vectors) - 1): + if vectors[i][1] == vectors[i+1][0] or vectors[i][1] + 1== vectors[i+1][0]: + vectors.append((vectors[i][0], vectors[i+1][1])) + vectors.pop(i+1) + vectors.pop(i) + vectors = sorted(vectors) + return False, vectors + elif len(vectors) == 2: + if vectors[0][1] == vectors[1][0] or vectors[0][1] + 1== vectors[1][0]: + vectors = [(vectors[0][0], vectors[1][1])] + return True, vectors + return True, vectors + +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)) + for j in range(distance + 1): + CoveredPoints.setdefault((sensY-j), []) + CoveredPoints.setdefault((sensY+j), []) + CoveredPoints[sensY-j].append((sensX - (distance - j), sensX + (distance - j))) + CoveredPoints[sensY+j].append((sensX - (distance - j), sensX + (distance - j))) + + +tempA = [] + +for vc in CoveredPoints: + done = False + vectors = CoveredPoints[vc].copy() + while not done: + done, vectors = loop(vectors) + done = False + vectors = sorted(vectors) + while not done: + done, vectors = loop2(vectors) + CoveredPoints[vc] = vectors + +for i in range(maxcoord + 1): + line = CoveredPoints[i] + if len(line) > 1: + coordX = line[0][1] + 1 + coordY = i + frequency = coordX * 4000000 + coordY + +print('Beacon found y =',coordY,'x =',coordX,'frequency =', frequency) diff --git a/in.txt b/in.txt index 1926028..652e631 100644 --- a/in.txt +++ b/in.txt @@ -1,2 +1,14 @@ -498,4 -> 498,6 -> 496,6 -503,4 -> 502,4 -> 502,9 -> 494,9 \ No newline at end of file +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/input15.txt b/input15.txt new file mode 100644 index 0000000..810df95 --- /dev/null +++ b/input15.txt @@ -0,0 +1,27 @@ +Sensor at x=2288642, y=2282562: closest beacon is at x=1581951, y=2271709 +Sensor at x=2215505, y=2975419: closest beacon is at x=2229474, y=3709584 +Sensor at x=275497, y=3166843: closest beacon is at x=-626874, y=3143870 +Sensor at x=1189444, y=2115305: closest beacon is at x=1581951, y=2271709 +Sensor at x=172215, y=2327851: closest beacon is at x=-101830, y=2000000 +Sensor at x=3953907, y=1957660: closest beacon is at x=2882446, y=1934422 +Sensor at x=685737, y=2465261: closest beacon is at x=1581951, y=2271709 +Sensor at x=1458348, y=2739442: closest beacon is at x=1581951, y=2271709 +Sensor at x=3742876, y=2811554: closest beacon is at x=3133845, y=3162635 +Sensor at x=437819, y=638526: closest beacon is at x=-101830, y=2000000 +Sensor at x=2537979, y=1762726: closest beacon is at x=2882446, y=1934422 +Sensor at x=1368739, y=2222863: closest beacon is at x=1581951, y=2271709 +Sensor at x=2743572, y=3976937: closest beacon is at x=2229474, y=3709584 +Sensor at x=2180640, y=105414: closest beacon is at x=3011118, y=-101788 +Sensor at x=3845753, y=474814: closest beacon is at x=3011118, y=-101788 +Sensor at x=2493694, y=3828087: closest beacon is at x=2229474, y=3709584 +Sensor at x=2786014, y=3388077: closest beacon is at x=3133845, y=3162635 +Sensor at x=3593418, y=3761871: closest beacon is at x=3133845, y=3162635 +Sensor at x=856288, y=3880566: closest beacon is at x=2229474, y=3709584 +Sensor at x=1757086, y=2518373: closest beacon is at x=1581951, y=2271709 +Sensor at x=2853518, y=2939097: closest beacon is at x=3133845, y=3162635 +Sensor at x=1682023, y=1449902: closest beacon is at x=1581951, y=2271709 +Sensor at x=3360575, y=1739100: closest beacon is at x=2882446, y=1934422 +Sensor at x=2904259, y=1465606: closest beacon is at x=2882446, y=1934422 +Sensor at x=3078500, y=3564862: closest beacon is at x=3133845, y=3162635 +Sensor at x=2835288, y=1011055: closest beacon is at x=2882446, y=1934422 +Sensor at x=2998762, y=2414323: closest beacon is at x=2882446, y=1934422 \ No newline at end of file