2021-12-09 12:35:26 +00:00
|
|
|
(import (chicken string))
|
|
|
|
(import (chicken format))
|
|
|
|
(import (chicken io))
|
|
|
|
(import (chicken sort))
|
|
|
|
(import srfi-141)
|
|
|
|
|
|
|
|
(define numbers (map string->number (string-split (read-line) ",")))
|
|
|
|
|
|
|
|
; PART 1
|
2021-12-09 12:55:14 +00:00
|
|
|
(let* ((sorted (sort numbers <))
|
|
|
|
(median (list-ref sorted (floor/ (length numbers) 2)))
|
2021-12-09 13:00:15 +00:00
|
|
|
(part1 (foldl (lambda (x y) (+ x (abs (- y median))))
|
|
|
|
0 numbers)))
|
2021-12-09 12:55:14 +00:00
|
|
|
(format #t "part 1: ~A~%" part1))
|
2021-12-09 12:35:26 +00:00
|
|
|
|
|
|
|
; PART 2
|
|
|
|
(define (triangular n) (/ (* n (+ n 1)) 2))
|
|
|
|
|
|
|
|
(define (part2- avg)
|
2021-12-09 12:55:14 +00:00
|
|
|
(foldl (lambda (x y) (+ x (triangular (abs (- y avg)))))
|
2021-12-09 13:00:15 +00:00
|
|
|
0 numbers))
|
2021-12-09 12:55:14 +00:00
|
|
|
|
|
|
|
(let* ((len (length numbers))
|
|
|
|
(sum (foldl + 0 numbers))
|
|
|
|
(avg-ceil (ceiling/ sum len))
|
|
|
|
(avg-floor (floor/ sum len))
|
|
|
|
(part2 (min (part2- avg-ceil) (part2- avg-floor))))
|
|
|
|
(format #t "part 2: ~A~%" part2))
|