aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol.scm (1068B) [raw]


      1 #!/usr/local/bin/chicken-csi -s
      2 (import (chicken io)
      3         (chicken sort))
      4 
      5 ;; A simple take on part-1 in constant memory
      6 (define (part-1)
      7   (let loop ((curr 0) (max_ 0) (line (read-line)))
      8     (if (eof-object? line)
      9         (max curr max_)
     10         (if (equal? line "")
     11             (loop 0 (max curr max_) (read-line))
     12             (loop (+ curr (string->number line)) max_ (read-line))))))
     13 
     14 ; (print (part-1))
     15 
     16 
     17 ;; For part 2, it's probably cleanest to jump straight to the "N" case
     18 
     19 (define (subseq lst i)   ; there's def an egg for this
     20   (if (equal? i 0)
     21       '()
     22       (cons (car lst) (subseq (cdr lst) (sub1 i)))))
     23 
     24 (define (condense-weights weights)
     25   (let loop ((curr 0) (condensed '()) (weights weights))
     26      (cond ((null? weights) (cons curr condensed))
     27            ((not (car weights))
     28             (loop 0 (cons curr condensed) (cdr weights)))
     29            (else (loop (+ curr (car weights)) condensed (cdr weights))))))
     30 
     31 (let ((weights (map string->number (read-lines))))
     32   (print (apply +
     33                 (subseq (sort (condense-weights weights) >) 3))))