2021/day02.scm

49 lines
1.7 KiB
Scheme
Raw Normal View History

2021-12-09 19:04:48 +00:00
(import (chicken io))
(import (chicken format))
(import (chicken string))
(import (chicken condition))
(import srfi-1)
(define (parse-line line)
(let-values (((command offset) (apply values (string-split line))))
(list command (string->number offset))))
(define (parse inputs)
(map parse-line (read-lines)))
(define (part1-direction head tail h d)
(let-values (((command offset) (apply values head)))
(cond ((equal? command "forward")
(part1 tail (+ h offset) d))
((equal? command "down")
(part1 tail h (+ d offset)))
((equal? command "up")
(part1 tail h (- d offset)))
(else (abort (format "did not expect command '~A'" command))))))
(define (part1 directions h d)
(if (not (null? directions))
(part1-direction (car directions) (cdr directions) h d)
(* h d)))
(define (part2-direction head tail horiz depth aim)
(let-values (((command offset) (apply values head)))
(cond ((equal? (car command) "forward")
(part2 tail (+ horiz offset) (+ depth (* aim offset)) aim))
((equal? command "down")
(part2 tail horiz depth (+ aim offset)))
((equal? command "up")
(part2 tail horiz depth (- aim offset)))
(else (abort (format "did not expect command '~A'" command))))))
(define (part2 directions horiz depth aim)
(if (not (null? directions))
(part2-direction (car directions) (cdr directions) horiz depth aim)
(* horiz depth)))
(let ((directions (parse '())))
(format #t "part1 = ~A~%" (part1 directions 0 0))
(format #t "part2 = ~A~%" (part2 directions 0 0 0)))