sol.scm (950B) [raw]
1 (import (chicken io)) 2 3 (define (read-num port) 4 (let ((line (read-line port))) 5 (if (eof-object? line) 6 #f 7 (string->number line)))) 8 9 (define (part-1 port) 10 (let loop ((total 0) (prev #f) (num (read-num port))) 11 (define (next) 12 (loop total num (read-num port))) 13 (cond ((not num) total) 14 ((not prev) (next)) 15 ((< prev num) (loop (add1 total) num (read-num port))) 16 (else (next))))) 17 18 (define (winsum vect) 19 (apply + (vector->list vect))) 20 21 (define (part-2 port) 22 (let loop ((i 0) (total 0) (window #(0 0 0)) (num (read-num port))) 23 (if (not num) 24 total 25 (let ((old-val (winsum window))) 26 (vector-set! window (modulo i 3) num) 27 (if (and (> i 2) (< old-val (winsum window))) 28 (loop (add1 i) (add1 total) window (read-num port)) 29 (loop (add1 i) total window (read-num port))))))) 30 31 (call-with-input-file "input" 32 (lambda (p) (print (part-1 p)))) 33 34 (call-with-input-file "input" 35 (lambda (p) (print (part-2 p))))