44 lines
1.3 KiB
Racket
44 lines
1.3 KiB
Racket
#lang curly-fn racket
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
;; solution for day 9
|
|
|
|
;; helper functions here
|
|
|
|
;; linear time sum checker, assuming sorted subrange
|
|
(define (contains-sum? vec start end wanted-sum)
|
|
(define (helper [a start] [b (sub1 end)])
|
|
(define cur-sum (+ (vector-ref vec a) (vector-ref vec b)))
|
|
(cond
|
|
[(>= a b) #f]
|
|
[(= cur-sum wanted-sum) #t]
|
|
[(< cur-sum wanted-sum) (helper (add1 a) b)]
|
|
[(> cur-sum wanted-sum) (helper a (sub1 b))]))
|
|
(vector-sort! vec < start end)
|
|
(helper))
|
|
|
|
;; continuously sort the part of the vector below the iterator, and call contains-sum?
|
|
(define (part1 vec)
|
|
(for*/first ([i (in-range 25 (vector-length vec))] [x (in-value (vector-ref vec i))]
|
|
#:unless (contains-sum? vec 0 i x))
|
|
(cons i x)))
|
|
|
|
(define (part2 vec pt1-ans)
|
|
(match-define (cons pt1-i pt1-val) pt1-ans)
|
|
(for*/first ([ct (in-range 3 100)] [i (in-range 0 (- pt1-i ct))]
|
|
[sub (in-value (vector-copy vec i (+ i ct)))]
|
|
#:when (= pt1-val (for/fold ([x 0]) ([v (in-vector sub)]) (+ x v))))
|
|
(+ (vector-argmin identity sub) (vector-argmax identity sub))))
|
|
|
|
(module+ main
|
|
(define input (list->vector (file->list "inputs/9")))
|
|
;; part 1
|
|
(define pt1 (part1 (vector-copy input)))
|
|
(answer 9 1 (cdr pt1))
|
|
|
|
;; part 2
|
|
(answer 9 2 (part2 (vector-copy input) pt1))
|
|
|
|
(displayln "meow"))
|