From e050f6b6543c9ca839a0327cbfe9279d5542f06e Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 1 Dec 2016 00:23:05 -0800 Subject: [PATCH] tweak --- 2016/day01.rkt | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/2016/day01.rkt b/2016/day01.rkt index c51ee77..705acd7 100644 --- a/2016/day01.rkt +++ b/2016/day01.rkt @@ -15,19 +15,21 @@ (define (solve . turns) (define loc - (let loop ([loc 0] - [dir 1] - [turns turns]) - (if (empty? turns) - loc - (let* ([turn (car turns)] - [rotation (car turn)] - [dist (cdr turn)] - [new-dir (* dir rotation)]) - (loop (+ loc (* new-dir dist)) new-dir (cdr turns)))))) - (+ (abs (imag-part loc)) (abs (real-part loc)))) + (let loop ([loc 0] + [dir 1] + [turns turns]) + (if (empty? turns) + loc + (let* ([turn (car turns)] + [rotation (car turn)] + [dist (cdr turn)] + [new-dir (* dir rotation)]) + (loop (+ loc (* new-dir dist)) new-dir (cdr turns)))))) + (+ (abs (imag-part loc)) (abs (real-part loc)))) -(define-macro-cases turn - [(turn "L" DIST) #'(cons +i (string->number DIST))] - [(turn "R" DIST) #'(cons -i (string->number DIST))]) +(define-macro (turn DIR DIST) + (with-pattern ([NEW-DIR (syntax-case #'DIR () + ["L" #'+i] + ["R" #'-i])]) + #'(cons NEW-DIR (string->number DIST)))) (provide turn) \ No newline at end of file