diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2022/day-15 | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'aoc2022/day-15')
-rw-r--r-- | aoc2022/day-15/day-15.rkt | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/aoc2022/day-15/day-15.rkt b/aoc2022/day-15/day-15.rkt deleted file mode 100644 index b050807..0000000 --- a/aoc2022/day-15/day-15.rkt +++ /dev/null @@ -1,54 +0,0 @@ -#lang racket - -(require advent-of-code - threading - fancy-app - algorithms - (prefix-in iset- data/integer-set)) - -(struct beacon-record (sensor beacon)) -(struct posn (x y)) - -(define beacon-records - (~> (fetch-aoc-input (find-session) 2022 15 #:cache #true) - (string-split "\n") - (map (λ~> (string-replace #px"[^\\d\\s-]" "") - string-split - (map string->number _) - (chunks-of 2) - (map (apply posn _) _) - (apply beacon-record _)) - _))) - -(define (manhattan-distance-to-beacon record) - (match-define (beacon-record (posn sx sy) (posn bx by)) record) - (+ (abs (- sx bx)) (abs (- sy by)))) - -(define (coverage-at-row record row) - (match-define (beacon-record (posn sx sy) _) record) - (define x-distance (- (manhattan-distance-to-beacon record) (abs (- row sy)))) - (cond - [(negative? x-distance) (iset-make-range)] - [else (iset-make-range (- sx x-distance) (+ sx x-distance))])) - -(define (total-coverage-at-row records row) - (for/fold ([coverage (iset-make-range)]) ([r (in-list records)]) - (iset-union coverage (coverage-at-row r row)))) - -;; part 1 -(define (coverage-without-beacons records row) - (~> (total-coverage-at-row records row) - iset-count - (- (count (λ (b) (= row (posn-y b))) - (~> beacon-records (map beacon-record-beacon _) remove-duplicates))))) - -(coverage-without-beacons beacon-records 2000000) - -;; part 2 -(define (find-only-beacon beacon-records size) - (for/first ([y (in-range 0 size)] - #:do [(define xs (iset-complement (total-coverage-at-row beacon-records y) 0 size))] - #:when (= 1 (iset-count xs))) - (+ (* 4000000 (iset-get-integer xs)) y))) - -(find-only-beacon beacon-records 4000000) |