Day 9.
This commit is contained in:
parent
4ce6e3bb0c
commit
1b065d7cce
File diff suppressed because it is too large
Load Diff
6
lib.rkt
6
lib.rkt
|
@ -43,7 +43,7 @@
|
|||
number->digits-reverse
|
||||
digits->number
|
||||
|
||||
rac
|
||||
snoc
|
||||
scanl scanr
|
||||
list-ref*
|
||||
repeat
|
||||
|
@ -228,9 +228,9 @@
|
|||
|
||||
;; List helpers ;;
|
||||
|
||||
;; rac : (listof any) -> any -> (listof any)
|
||||
;; snoc : (listof any) -> any -> (listof any)
|
||||
;; Append element to the back of the list.
|
||||
(define (rac lst v)
|
||||
(define (snoc lst v)
|
||||
(append lst (list v)))
|
||||
|
||||
;; scanl : (a -> a -> a) -> (listof a) -> (listof a)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
#lang racket
|
||||
|
||||
(require "../lib.rkt")
|
||||
|
||||
(define input (map string->number (problem-input 9)))
|
||||
|
||||
(define (pair-sum lst target)
|
||||
(for*/or ([i lst]
|
||||
[j lst])
|
||||
(= (+ i j) target)))
|
||||
|
||||
(define part1
|
||||
(let loop ([current (take input 25)] [next (list-ref input 25)] [remain (drop input 26)])
|
||||
(if (pair-sum current next)
|
||||
(loop (snoc (rest current) next) (first remain) (rest remain))
|
||||
next)))
|
||||
|
||||
(define part2
|
||||
(let loop ([current '()] [remain input] [sum 0])
|
||||
(cond
|
||||
[(< sum part1)
|
||||
(loop (snoc current (first remain)) (rest remain) (+ sum (first remain)))]
|
||||
[(> sum part1)
|
||||
(loop (rest current) remain (- sum (first current)))]
|
||||
[else (+ (apply min current) (apply max current))])))
|
||||
|
||||
(show-solution part1 part2)
|
Loading…
Reference in New Issue