This commit is contained in:
Jonathan Chan 2020-12-08 21:49:59 -08:00
parent c0affdfc3c
commit bc98c92d53
3 changed files with 1030 additions and 3 deletions

1000
input/09.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,7 @@
number->digits-reverse number->digits-reverse
digits->number digits->number
rac snoc
scanl scanr scanl scanr
list-ref* list-ref*
repeat repeat
@ -228,9 +228,9 @@
;; List helpers ;; ;; List helpers ;;
;; rac : (listof any) -> any -> (listof any) ;; snoc : (listof any) -> any -> (listof any)
;; Append element to the back of the list. ;; Append element to the back of the list.
(define (rac lst v) (define (snoc lst v)
(append lst (list v))) (append lst (list v)))
;; scanl : (a -> a -> a) -> (listof a) -> (listof a) ;; scanl : (a -> a -> a) -> (listof a) -> (listof a)

27
src/09.rkt Normal file
View File

@ -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)