#!/usr/local/bin/chicken-csi -s (import (chicken io) (chicken sort)) ;; A simple take on part-1 in constant memory (define (part-1) (let loop ((curr 0) (max_ 0) (line (read-line))) (if (eof-object? line) (max curr max_) (if (equal? line "") (loop 0 (max curr max_) (read-line)) (loop (+ curr (string->number line)) max_ (read-line)))))) ; (print (part-1)) ;; For part 2, it's probably cleanest to jump straight to the "N" case (define (subseq lst i) ; there's def an egg for this (if (equal? i 0) '() (cons (car lst) (subseq (cdr lst) (sub1 i))))) (define (condense-weights weights) (let loop ((curr 0) (condensed '()) (weights weights)) (cond ((null? weights) (cons curr condensed)) ((not (car weights)) (loop 0 (cons curr condensed) (cdr weights))) (else (loop (+ curr (car weights)) condensed (cdr weights)))))) (let ((weights (map string->number (read-lines)))) (print (apply + (subseq (sort (condense-weights weights) >) 3))))