2021/day01.scm

31 lines
875 B
Scheme
Raw Normal View History

2021-12-09 19:04:48 +00:00
(import (chicken io))
(import (chicken format))
(import srfi-1)
(define (parse inputs)
(let ((line (read-line)))
(if (not (eof-object? line))
(parse (cons (string->number line) inputs))
inputs)))
(define (inc-if cond num)
(if cond (+ num 1) num))
(define (part1 inputs previous counter)
(if (not (null? inputs))
(let ((head (car inputs)))
(part1 (cdr inputs) head (inc-if (<= head previous) counter)))
counter))
(define (part2 inputs previous counter)
(if (not (eq? inputs '()))
(let ((sum (apply + (take-right! inputs 3))))
(part2 (cdr inputs) sum (inc-if (<= sum previous) counter)))
counter))
(let ((inputs (parse '())))
(format #t "inputs = ~A~%" inputs)
(format #t "part1 = ~A~%" (part1 inputs 0 0))
(format #t "part2 = ~A~%" (part2 inputs 0 0)))