day 7
This commit is contained in:
parent
8ba61ad00d
commit
a1c8126274
2 changed files with 44 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,5 @@
|
|||
*.in
|
||||
.metals/
|
||||
|
||||
# binaries
|
||||
day??
|
||||
|
|
41
day07.scm
Normal file
41
day07.scm
Normal file
|
@ -0,0 +1,41 @@
|
|||
(import (chicken string))
|
||||
(import (chicken format))
|
||||
(import (chicken io))
|
||||
(import (chicken sort))
|
||||
(import (only traversal enumerate))
|
||||
(import srfi-1)
|
||||
(import srfi-14)
|
||||
(import srfi-60)
|
||||
(import srfi-141)
|
||||
|
||||
(define numbers (map string->number (string-split (read-line) ",")))
|
||||
|
||||
; PART 1
|
||||
(define sorted (sort numbers <))
|
||||
(define median (list-ref sorted (floor/ (length numbers) 2)))
|
||||
|
||||
(define (part1)
|
||||
(foldl
|
||||
(lambda (x y) (+ x (abs (- y median))))
|
||||
0
|
||||
numbers))
|
||||
|
||||
|
||||
; PART 2
|
||||
(define (triangular n) (/ (* n (+ n 1)) 2))
|
||||
|
||||
(define (part2- avg)
|
||||
(foldl
|
||||
(lambda (x y) (+ x (triangular (abs (- y avg)))))
|
||||
0
|
||||
numbers))
|
||||
|
||||
(define avgCeil (ceiling/ (foldl + 0 numbers) (length numbers)))
|
||||
(define avgFloor (floor/ (foldl + 0 numbers) (length numbers)))
|
||||
|
||||
(define (part2)
|
||||
(min (part2- avgCeil) (part2- avgFloor)))
|
||||
|
||||
|
||||
(format #t "part 1: ~A~%" (part1))
|
||||
(format #t "part 2: ~A~%" (part2))
|
Loading…
Reference in a new issue