diff --git a/input/15.txt b/input/15.txt new file mode 100644 index 0000000..7cdce8a --- /dev/null +++ b/input/15.txt @@ -0,0 +1,26 @@ +Sensor at x=2483411, y=3902983: closest beacon is at x=2289579, y=3633785 +Sensor at x=3429446, y=303715: closest beacon is at x=2876111, y=-261280 +Sensor at x=666423, y=3063763: closest beacon is at x=2264411, y=2779977 +Sensor at x=3021606, y=145606: closest beacon is at x=2876111, y=-261280 +Sensor at x=2707326, y=2596893: closest beacon is at x=2264411, y=2779977 +Sensor at x=3103704, y=1560342: closest beacon is at x=2551409, y=2000000 +Sensor at x=3497040, y=3018067: closest beacon is at x=3565168, y=2949938 +Sensor at x=1708530, y=855013: closest beacon is at x=2551409, y=2000000 +Sensor at x=3107437, y=3263465: closest beacon is at x=3404814, y=3120160 +Sensor at x=2155249, y=2476196: closest beacon is at x=2264411, y=2779977 +Sensor at x=3447897, y=3070850: closest beacon is at x=3404814, y=3120160 +Sensor at x=2643048, y=3390796: closest beacon is at x=2289579, y=3633785 +Sensor at x=3533132, y=3679388: closest beacon is at x=3404814, y=3120160 +Sensor at x=3683790, y=3017900: closest beacon is at x=3565168, y=2949938 +Sensor at x=1943208, y=3830506: closest beacon is at x=2289579, y=3633785 +Sensor at x=3940100, y=3979653: closest beacon is at x=2846628, y=4143786 +Sensor at x=3789719, y=1225738: closest beacon is at x=4072555, y=1179859 +Sensor at x=3939775, y=578381: closest beacon is at x=4072555, y=1179859 +Sensor at x=3880152, y=3327397: closest beacon is at x=3404814, y=3120160 +Sensor at x=3280639, y=2446475: closest beacon is at x=3565168, y=2949938 +Sensor at x=2348869, y=2240374: closest beacon is at x=2551409, y=2000000 +Sensor at x=3727441, y=2797456: closest beacon is at x=3565168, y=2949938 +Sensor at x=3973153, y=2034945: closest beacon is at x=4072555, y=1179859 +Sensor at x=38670, y=785556: closest beacon is at x=311084, y=-402911 +Sensor at x=3181909, y=2862960: closest beacon is at x=3565168, y=2949938 +Sensor at x=3099490, y=3946226: closest beacon is at x=2846628, y=4143786 diff --git a/src/15.rkt b/src/15.rkt new file mode 100644 index 0000000..2b4a474 --- /dev/null +++ b/src/15.rkt @@ -0,0 +1,54 @@ +#lang curly-fn racket + +(require "../lib.rkt" + data/integer-set) + +(define input + (for/list ([positions (problem-input 15)]) + (match-define (pregexp "Sensor at x=(-?\\d+), y=(-?\\d+): closest beacon is at x=(-?\\d+), y=(-?\\d+)" sb) + positions) + (map string->number (rest sb)))) + +(define y 2000000) +(define bound 4000000) + +(define (manhat x1 y1 x2 y2) + (+ (abs (- x1 x2)) (abs (- y1 y2)))) + +(define sensors + (for/list ([sb input]) + (match-define `(,sx ,sy ,bx ,by) sb) + (list sx sy (manhat sx sy bx by)))) + +(define (impossible? x y) + (for/or ([sensor sensors]) + (match-define `(,sx ,sy ,d) sensor) + (<= (manhat sx sy x y) d))) + +(define part1 + (for/fold ([impossibles (make-range)] + #:result (count impossibles)) + ([sb input]) + (match-define `(,sx ,sy ,bx ,by) sb) + (define span (- (manhat sx sy bx by) (abs (- sy y)))) + (if (> span 0) + (~> impossibles + (union (make-range (- sx span) (+ sx span))) + (subtract (if (= by y) (make-range bx) (make-range)))) + impossibles))) + +(define part2 + (for/or ([sensor sensors]) + (match-define `(,sx ,sy ,d) sensor) + (define d* (add1 d)) + (for/or ([x (in-inclusive-range (- sx d*) (+ sx d*))] + #:when (<= 0 x bound)) + (define +y (+ sy (- d* (- sx x)))) + (define -y (- sy (- d* (- sx x)))) + (define (freq y) + (and (<= 0 y bound) + (not (impossible? x y)) + (+ (* x 4000000) y))) + (or (freq +y) (freq -y))))) + +(show-solution part1 part2) \ No newline at end of file