Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

35
2021/day07/day07.scm Normal file
View file

@ -0,0 +1,35 @@
(import (chicken io)
(chicken sort)
srfi-1
srfi-152)
(define (read-input #!optional (port (current-input-port)))
(map string->number (string-split (read-line port) ",")))
(define (median lst)
(list-ref (sort lst <=) (/ (length lst) 2)))
(define (part1 in)
(let ((target-pos (median in)))
(apply + (map (lambda (x) (abs (- x target-pos))) in))))
(define (cost start end)
(let ((dist (abs (- start end))))
(/ (* dist (+ dist 1)) 2)))
(define (total-fuel target-pos in)
(apply + (map (lambda (x) (cost target-pos x)) in)))
(define (part2 in)
(apply min
(let ((min-in (apply min in))
(max-in (apply max in)))
(let lp ((pos min-in)
(fuels '()))
(if (> pos max-in)
fuels
(lp (+ 1 pos) (cons (total-fuel pos in) fuels)))))))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))