Refactored Day 9.
This commit is contained in:
parent
b7faf7c961
commit
31d972fe31
28
src/09.rkt
28
src/09.rkt
|
@ -3,25 +3,29 @@
|
||||||
(require "../lib.rkt")
|
(require "../lib.rkt")
|
||||||
|
|
||||||
(define input (map string->number (problem-input 9)))
|
(define input (map string->number (problem-input 9)))
|
||||||
|
(define input* (list->vector input))
|
||||||
|
|
||||||
(define (pair-sum lst target)
|
(define (pair-sum start end)
|
||||||
(for*/or ([i lst]
|
(for*/or ([i (range start end)]
|
||||||
[j lst])
|
[j (range start end)])
|
||||||
(= (+ i j) target)))
|
(= (vector-ref input* end)
|
||||||
|
(+ (vector-ref input* i)
|
||||||
|
(vector-ref input* j)))))
|
||||||
|
|
||||||
(define part1
|
(define part1
|
||||||
(let loop ([current (take input 25)] [next (list-ref input 25)] [remain (drop input 26)])
|
(let loop ([start 0] [end 25])
|
||||||
(if (pair-sum current next)
|
(if (pair-sum start end)
|
||||||
(loop (snoc (rest current) next) (first remain) (rest remain))
|
(loop (add1 start) (add1 end))
|
||||||
next)))
|
(vector-ref input* end))))
|
||||||
|
|
||||||
(define part2
|
(define part2
|
||||||
(let loop ([current '()] [remain input] [sum 0])
|
(let loop ([start 0] [end 0] [sum 0])
|
||||||
(cond
|
(cond
|
||||||
[(< sum part1)
|
[(< sum part1)
|
||||||
(loop (snoc current (first remain)) (rest remain) (+ sum (first remain)))]
|
(loop start (add1 end) (+ sum (vector-ref input* end)))]
|
||||||
[(> sum part1)
|
[(> sum part1)
|
||||||
(loop (rest current) remain (- sum (first current)))]
|
(loop (add1 start) end (- sum (vector-ref input* start)))]
|
||||||
[else (+ (apply min current) (apply max current))])))
|
[else (let ([contig (drop (take input end) start)])
|
||||||
|
(+ (apply min contig) (apply max contig)))])))
|
||||||
|
|
||||||
(show-solution part1 part2)
|
(show-solution part1 part2)
|
||||||
|
|
Loading…
Reference in New Issue