(import (chicken io) (chicken string)) (define (part-1 port) (let loop ((h 0) (d 0)) (let ((l (read-line port))) (if (eof-object? l) (* h d) (let* ((parts (string-split l)) (act (car parts)) (mag (string->number (cadr parts)))) (cond ((equal? act "down") (loop h (+ d mag))) ((equal? act "up") (loop h (- d mag))) ((equal? act "forward") (loop (+ h mag) d)) (else (error "Bad action " act)))))))) (define (part-2 port) (let loop ((h 0) (d 0) (a 0)) (let ((l (read-line port))) (if (eof-object? l) (* h d) (let* ((parts (string-split l)) (act (car parts)) (mag (string->number (cadr parts)))) (cond ((equal? act "down") (loop h d (+ a mag))) ((equal? act "up") (loop h d (- a mag))) ((equal? act "forward") (loop (+ h mag) (+ d (* a mag)) a)) (else (error "Bad action " act)))))))) (call-with-input-file "input" (lambda (p) (print (part-1 p)))) (call-with-input-file "input" (lambda (p) (print (part-2 p))))