2020-12-09 05:17:51 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 9
|
|
|
|
|
|
|
|
;; helper functions here
|
|
|
|
|
2020-12-09 05:54:22 +00:00
|
|
|
;; 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))))
|
2020-12-09 05:17:51 +00:00
|
|
|
|
|
|
|
(module+ main
|
2020-12-09 05:54:22 +00:00
|
|
|
(define input (list->vector (file->list "inputs/9")))
|
2020-12-09 05:17:51 +00:00
|
|
|
;; part 1
|
2020-12-09 05:54:22 +00:00
|
|
|
(define pt1 (part1 (vector-copy input)))
|
|
|
|
(answer 9 1 (cdr pt1))
|
2020-12-09 05:17:51 +00:00
|
|
|
|
|
|
|
;; part 2
|
2020-12-09 05:54:22 +00:00
|
|
|
(answer 9 2 (part2 (vector-copy input) pt1))
|
2020-12-09 05:17:51 +00:00
|
|
|
|
|
|
|
(displayln "meow"))
|