aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol.scm (1298B) [raw]


      1 (import (chicken io)
      2         (chicken string))
      3 
      4 (define (part-1 port)
      5   (let loop ((h 0) (d 0))
      6     (let ((l (read-line port)))
      7       (if (eof-object? l)
      8         (* h d)
      9         (let* ((parts (string-split l))
     10                (act (car parts))
     11                (mag (string->number (cadr parts))))
     12           (cond ((equal? act "down")
     13                  (loop h (+ d mag)))
     14                 ((equal? act "up")
     15                  (loop h (- d mag)))
     16                 ((equal? act "forward")
     17                  (loop (+ h mag) d))
     18                 (else (error "Bad action " act))))))))
     19 
     20 (define (part-2 port)
     21   (let loop ((h 0) (d 0) (a 0))
     22     (let ((l (read-line port)))
     23       (if (eof-object? l)
     24         (* h d)
     25         (let* ((parts (string-split l))
     26                (act (car parts))
     27                (mag (string->number (cadr parts))))
     28           (cond ((equal? act "down")
     29                  (loop h d (+ a mag)))
     30                 ((equal? act "up")
     31                  (loop h d (- a mag)))
     32                 ((equal? act "forward")
     33                  (loop (+ h mag) (+ d (* a mag)) a))
     34                 (else (error "Bad action " act))))))))
     35 
     36 
     37 (call-with-input-file "input"
     38                       (lambda (p) (print (part-1 p))))
     39 
     40 (call-with-input-file "input"
     41                       (lambda (p) (print (part-2 p))))