day07: make a bit more idiomatic (i think)
This commit is contained in:
parent
d4aba82554
commit
299ccb33d0
1 changed files with 16 additions and 22 deletions
32
day07.scm
32
day07.scm
|
@ -7,31 +7,25 @@
|
||||||
(define numbers (map string->number (string-split (read-line) ",")))
|
(define numbers (map string->number (string-split (read-line) ",")))
|
||||||
|
|
||||||
; PART 1
|
; PART 1
|
||||||
(define sorted (sort numbers <))
|
(let* ((sorted (sort numbers <))
|
||||||
(define median (list-ref sorted (floor/ (length numbers) 2)))
|
(median (list-ref sorted (floor/ (length numbers) 2)))
|
||||||
|
(part1
|
||||||
(define (part1)
|
(foldl (lambda (x y) (+ x (abs (- y median))))
|
||||||
(foldl
|
|
||||||
(lambda (x y) (+ x (abs (- y median))))
|
|
||||||
0
|
0
|
||||||
numbers))
|
numbers)))
|
||||||
|
(format #t "part 1: ~A~%" part1))
|
||||||
|
|
||||||
; PART 2
|
; PART 2
|
||||||
(define (triangular n) (/ (* n (+ n 1)) 2))
|
(define (triangular n) (/ (* n (+ n 1)) 2))
|
||||||
|
|
||||||
(define (part2- avg)
|
(define (part2- avg)
|
||||||
(foldl
|
(foldl (lambda (x y) (+ x (triangular (abs (- y avg)))))
|
||||||
(lambda (x y) (+ x (triangular (abs (- y avg)))))
|
|
||||||
0
|
0
|
||||||
numbers))
|
numbers))
|
||||||
|
|
||||||
(define avgCeil (ceiling/ (foldl + 0 numbers) (length numbers)))
|
(let* ((len (length numbers))
|
||||||
(define avgFloor (floor/ (foldl + 0 numbers) (length numbers)))
|
(sum (foldl + 0 numbers))
|
||||||
|
(avg-ceil (ceiling/ sum len))
|
||||||
(define (part2)
|
(avg-floor (floor/ sum len))
|
||||||
(min (part2- avgCeil) (part2- avgFloor)))
|
(part2 (min (part2- avg-ceil) (part2- avg-floor))))
|
||||||
|
(format #t "part 2: ~A~%" part2))
|
||||||
|
|
||||||
(format #t "part 1: ~A~%" (part1))
|
|
||||||
(format #t "part 2: ~A~%" (part2))
|
|
||||||
|
|
Loading…
Reference in a new issue