diff --git a/2022/full/day15.txt b/2022/full/day15.txt new file mode 100644 index 0000000..43321ab --- /dev/null +++ b/2022/full/day15.txt @@ -0,0 +1,31 @@ +Sensor at x=3859432, y=2304903: closest beacon is at x=3677247, y=3140958 +Sensor at x=2488890, y=2695345: closest beacon is at x=1934788, y=2667279 +Sensor at x=3901948, y=701878: closest beacon is at x=4095477, y=368031 +Sensor at x=2422190, y=1775708: closest beacon is at x=1765036, y=2000000 +Sensor at x=2703846, y=3282799: closest beacon is at x=2121069, y=3230302 +Sensor at x=172003, y=2579074: closest beacon is at x=-77667, y=3197309 +Sensor at x=1813149, y=1311283: closest beacon is at x=1765036, y=2000000 +Sensor at x=1704453, y=2468117: closest beacon is at x=1934788, y=2667279 +Sensor at x=1927725, y=2976002: closest beacon is at x=1934788, y=2667279 +Sensor at x=3176646, y=1254463: closest beacon is at x=2946873, y=2167634 +Sensor at x=2149510, y=3722117: closest beacon is at x=2121069, y=3230302 +Sensor at x=3804434, y=251015: closest beacon is at x=4095477, y=368031 +Sensor at x=2613561, y=3932220: closest beacon is at x=2121069, y=3230302 +Sensor at x=3997794, y=3291220: closest beacon is at x=3677247, y=3140958 +Sensor at x=98328, y=3675176: closest beacon is at x=-77667, y=3197309 +Sensor at x=2006541, y=2259601: closest beacon is at x=1934788, y=2667279 +Sensor at x=663904, y=122919: closest beacon is at x=1618552, y=-433244 +Sensor at x=1116472, y=3349728: closest beacon is at x=2121069, y=3230302 +Sensor at x=2810797, y=2300748: closest beacon is at x=2946873, y=2167634 +Sensor at x=1760767, y=2024355: closest beacon is at x=1765036, y=2000000 +Sensor at x=3098487, y=2529092: closest beacon is at x=2946873, y=2167634 +Sensor at x=1716839, y=634872: closest beacon is at x=1618552, y=-433244 +Sensor at x=9323, y=979154: closest beacon is at x=-245599, y=778791 +Sensor at x=1737623, y=2032367: closest beacon is at x=1765036, y=2000000 +Sensor at x=26695, y=3049071: closest beacon is at x=-77667, y=3197309 +Sensor at x=3691492, y=3766350: closest beacon is at x=3677247, y=3140958 +Sensor at x=730556, y=1657010: closest beacon is at x=1765036, y=2000000 +Sensor at x=506169, y=3958647: closest beacon is at x=-77667, y=3197309 +Sensor at x=2728744, y=23398: closest beacon is at x=1618552, y=-433244 +Sensor at x=3215227, y=3077078: closest beacon is at x=3677247, y=3140958 +Sensor at x=2209379, y=3030851: closest beacon is at x=2121069, y=3230302 diff --git a/2022/python/day15.py b/2022/python/day15.py index 538d9c5..59e53e0 100644 --- a/2022/python/day15.py +++ b/2022/python/day15.py @@ -6,9 +6,6 @@ from scanf import scanf from dataclasses import dataclass from scipy.spatial.distance import cityblock -def manhattan(a, b): - return abs(b[0] - a[0]) + abs(b[1] - a[1]) - @dataclass class Sensor: sX: int @@ -16,28 +13,57 @@ class Sensor: bX: int bY: int + oX: int + oY: int + @property def s(self): - return (self.sY, self.sX) + return (self.oY+self.sY, self.oX+self.sX) @property def b(self): - return (self.bY, self.bX) + return (self.oY+self.bY, self.oX+self.bX) @property def distance(self): a,b = zip(self.s, self.b) return cityblock(a, b) - def distance_to(self, y,x): + def distance_to(self, y, x): a,b = zip(self.s, (y,x)) return cityblock(a, b) + def on_line(self, y): + print(self.s) + midpoint = (y,self.s[1]) + d = self.distance_to(*midpoint) + if d > self.distance: + print() + return [] + + start = (y, midpoint[1] - abs(self.distance-d)) + end = (y, midpoint[1] + abs(self.distance-d)) + + assert self.distance_to(*start) == self.distance + print(self.distance_to(*start),'=',start[1],midpoint[1],end[1]) + print(self.distance_to(*end)) + print() + for x in range(start[1]+1, 9999): + d = self.distance_to(y, x) + print(x, d) + if d == self.distance: + print("X should be:", x) + break + print() + + + return list(range(start[1],end[1])) + + + #@shared.profile def part1(rows): - CHECK_ROW = 10 - CHECK_ROW = 2000000 sensors = [] sensor_points = [] beacon_points = [] @@ -49,29 +75,58 @@ def part1(rows): xSet.add(bx) ySet.add(y) ySet.add(by) - sensors.append(Sensor(sX=x,sY=y,bX=bx,bY=by)) + sensors.append(Sensor(sX=x,sY=y,bX=bx,bY=by,oY=0, oX=0)) minX, maxX = min(xSet),max(xSet) minY, maxY = min(ySet),max(ySet) + offsetX = 0 + offsetY = 0 + if minX < 0 or minY < 0: + o = max(abs(minX), abs(minY)) + offsetX = o + offsetY = o + for sensor in sensors: - sensor_points.append(sensor.s) + sensor.oY = offsetY + sensor.oX = offsetX beacon_points.append(sensor.b) + CHECK_ROW = 10 + offsetY + #CHECK_ROW = 2000000 + offsetY + + print(beacon_points) + print("") + ineligible = set() - for x in range(minX,maxX): - point = (CHECK_ROW, x) - if x % 10000 == 0: - print(point) - if point in beacon_points: + for y,x in beacon_points: + if y == CHECK_ROW: + ineligible.add(x) + mx = matrix.matrix_of_size(maxX+offsetX+1, maxY+offsetY+1) + for s in sensors: + coll = s.on_line(CHECK_ROW) + ineligible.update(coll) + #print(coll) + + for i in list(ineligible): + if i < 0: continue - if point in sensor_points: + if i > maxX+offsetX: continue - for s in sensors: - d = s.distance_to(*point) - if d <= s.distance: - ineligible.add(point) - pprint(ineligible) + mx[CHECK_ROW][i] = "#" + for s in sensors: + mx[s.s[0]][s.s[1]] = "S" + mx[s.b[0]][s.b[1]] = "B" + + #print(matrix.ppmx(mx, pad=False, space=False)) + print(" 1 1 2 2") + print(" 0 5 0 5 0 5") + matrix.view_matrix(mx, 11,0, 13, 30) + print("") + print("####B######################") + + print(ineligible) print(len(ineligible)) + # mx[sensor.sY][sensor.sX] = "S" # mx[sensor.bY][sensor.bX] = "B" diff --git a/2022/python/matrix.py b/2022/python/matrix.py index 9504fe9..c2edb50 100644 --- a/2022/python/matrix.py +++ b/2022/python/matrix.py @@ -304,8 +304,6 @@ def ppmx(*matrices, pad=True, space=True, zero="."): return "\n".join(out) def view_matrix(matrix, y1,x1, y2,x2): - print(y1,y2) - print(x1,x2) lines = ppmx(matrix, pad=0,space=0).split("\n") for line in lines[y1:y2+1]: print(line[x1:x2]) diff --git a/2022/samples/day15.txt b/2022/samples/day15.txt new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/2022/samples/day15.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