(import (chicken io)) (define (read-num port) (let ((line (read-line port))) (if (eof-object? line) #f (string->number line)))) (define (part-1 port) (let loop ((total 0) (prev #f) (num (read-num port))) (define (next) (loop total num (read-num port))) (cond ((not num) total) ((not prev) (next)) ((< prev num) (loop (add1 total) num (read-num port))) (else (next))))) (define (winsum vect) (apply + (vector->list vect))) (define (part-2 port) (let loop ((i 0) (total 0) (window #(0 0 0)) (num (read-num port))) (if (not num) total (let ((old-val (winsum window))) (vector-set! window (modulo i 3) num) (if (and (> i 2) (< old-val (winsum window))) (loop (add1 i) (add1 total) window (read-num port)) (loop (add1 i) total window (read-num port))))))) (call-with-input-file "input" (lambda (p) (print (part-1 p)))) (call-with-input-file "input" (lambda (p) (print (part-2 p))))