2016
Matthew Butterick 8 years ago
parent bc5f33df4d
commit e050f6b654

@ -15,19 +15,21 @@
(define (solve . turns) (define (solve . turns)
(define loc (define loc
(let loop ([loc 0] (let loop ([loc 0]
[dir 1] [dir 1]
[turns turns]) [turns turns])
(if (empty? turns) (if (empty? turns)
loc loc
(let* ([turn (car turns)] (let* ([turn (car turns)]
[rotation (car turn)] [rotation (car turn)]
[dist (cdr turn)] [dist (cdr turn)]
[new-dir (* dir rotation)]) [new-dir (* dir rotation)])
(loop (+ loc (* new-dir dist)) new-dir (cdr turns)))))) (loop (+ loc (* new-dir dist)) new-dir (cdr turns))))))
(+ (abs (imag-part loc)) (abs (real-part loc)))) (+ (abs (imag-part loc)) (abs (real-part loc))))
(define-macro-cases turn (define-macro (turn DIR DIST)
[(turn "L" DIST) #'(cons +i (string->number DIST))] (with-pattern ([NEW-DIR (syntax-case #'DIR ()
[(turn "R" DIST) #'(cons -i (string->number DIST))]) ["L" #'+i]
["R" #'-i])])
#'(cons NEW-DIR (string->number DIST))))
(provide turn) (provide turn)