Taddeus Kroes il y a 3 ans
Parent
commit
ec2b51cdbe
2 fichiers modifiés avec 67 ajouts et 0 suppressions
  1. 41 0
      2022/15_beacon.py
  2. 26 0
      2022/input/15

+ 41 - 0
2022/15_beacon.py

@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+import sys
+import re
+
+def ranges(sensors, y):
+    intervals = set()
+
+    def pop_overlapping(a, b):
+        for other in intervals:
+            c, d = other
+            if a <= d and c <= b:
+                intervals.remove(other)
+                return min(a, c), max(b, d)
+
+    for sx, sy, bx, by in sensors:
+        dist = abs(sx - bx) + abs(sy - by) - abs(sy - y)
+        if dist >= 0:
+            interval = sx - dist, sx + dist
+            new = pop_overlapping(*interval)
+            while new:
+                interval = new
+                new = pop_overlapping(*interval)
+            intervals.add(interval)
+
+    return intervals
+
+def coverage(sensors, y):
+    covered = sum(r - l + 1 for l, r in ranges(sensors, y))
+    beacons = len(set(bx for _, _, bx, by in sensors if by == y))
+    return covered - beacons
+
+def frequency(sensors, ymax):
+    for y in range(ymax + 1):
+        r = ranges(sensors, y)
+        if len(r) == 2:
+            (a, _), (b, _) = r
+            return (max(a, b) - 1) * 4000000 + y
+
+sensors = [tuple(map(int, re.findall(r'-?\d+', line))) for line in sys.stdin]
+print(coverage(sensors, 2000000))
+print(frequency(sensors, 4000000))

+ 26 - 0
2022/input/15

@@ -0,0 +1,26 @@
+Sensor at x=3772068, y=2853720: closest beacon is at x=4068389, y=2345925
+Sensor at x=78607, y=2544104: closest beacon is at x=-152196, y=4183739
+Sensor at x=3239531, y=3939220: closest beacon is at x=3568548, y=4206192
+Sensor at x=339124, y=989831: closest beacon is at x=570292, y=1048239
+Sensor at x=3957534, y=2132743: closest beacon is at x=3897332, y=2000000
+Sensor at x=1882965, y=3426126: closest beacon is at x=2580484, y=3654136
+Sensor at x=1159443, y=3861139: closest beacon is at x=2580484, y=3654136
+Sensor at x=2433461, y=287013: closest beacon is at x=2088099, y=-190228
+Sensor at x=3004122, y=3483833: closest beacon is at x=2580484, y=3654136
+Sensor at x=3571821, y=799602: closest beacon is at x=3897332, y=2000000
+Sensor at x=2376562, y=1539540: closest beacon is at x=2700909, y=2519581
+Sensor at x=785113, y=1273008: closest beacon is at x=570292, y=1048239
+Sensor at x=1990787, y=38164: closest beacon is at x=2088099, y=-190228
+Sensor at x=3993778, y=3482849: closest beacon is at x=4247709, y=3561264
+Sensor at x=3821391, y=3986080: closest beacon is at x=3568548, y=4206192
+Sensor at x=2703294, y=3999015: closest beacon is at x=2580484, y=3654136
+Sensor at x=1448314, y=2210094: closest beacon is at x=2700909, y=2519581
+Sensor at x=3351224, y=2364892: closest beacon is at x=4068389, y=2345925
+Sensor at x=196419, y=3491556: closest beacon is at x=-152196, y=4183739
+Sensor at x=175004, y=138614: closest beacon is at x=570292, y=1048239
+Sensor at x=1618460, y=806488: closest beacon is at x=570292, y=1048239
+Sensor at x=3974730, y=1940193: closest beacon is at x=3897332, y=2000000
+Sensor at x=2995314, y=2961775: closest beacon is at x=2700909, y=2519581
+Sensor at x=105378, y=1513086: closest beacon is at x=570292, y=1048239
+Sensor at x=3576958, y=3665667: closest beacon is at x=3568548, y=4206192
+Sensor at x=2712265, y=2155055: closest beacon is at x=2700909, y=2519581