From 72cfb197248025c282124122c7f4ee306fe0f347 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 1 Dec 2016 12:33:46 -0800 Subject: [PATCH] not working --- 2016/day01.rkt | 64 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/2016/day01.rkt b/2016/day01.rkt index 705acd7..999d930 100644 --- a/2016/day01.rkt +++ b/2016/day01.rkt @@ -3,33 +3,59 @@ (module+ reader (provide read-syntax) (define (read-syntax path port) + (define turn-strings (string-split (port->string port) ", ")) + (define turn-pattern #px"^([LR])(\\d+)$") + (define turn-datums + (for*/list ([tstr (in-list turn-strings)]) + (define match-result (regexp-match turn-pattern (string-trim tstr))) + `(turn ,@(cdr (or match-result empty))))) (strip-bindings #`(module day01-mod "day01.rkt" - #,@(for/list ([t (in-list (string-split (port->string port) ", "))]) - `(turn ,@(string-split (string-trim t) #px"(?<=[LR])"))))))) + #,@turn-datums)))) (define-macro (mb . TURNS) #'(#%module-begin (solve . TURNS))) (provide (rename-out [mb #%module-begin])) -(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)))))) +(define (loc-dist loc) (+ (abs (imag-part loc)) (abs (real-part loc)))) -(define-macro (turn DIR DIST) - (with-pattern ([NEW-DIR (syntax-case #'DIR () - ["L" #'+i] - ["R" #'-i])]) - #'(cons NEW-DIR (string->number DIST)))) +(define (locs-between loca locb) + (range loca (add1 locb))) + +(module+ test + (require rackunit) + (check-equal? (locs-between 0 3) '(0 1 2 3)) + (check-equal? (locs-between 3 0) (reverse (locs-between 0 3))) + (check-equal? (locs-between +0i +3i) '(+0i +1i +2i +3i)) + (check-equal? (locs-between +3i +0i) (reverse (locs-between +0i +3i)))) + +(define (solve . turns) + (define first-loc-visited-twice #f) + (define north 0+1i) + (let loop ([locs (list 0+0i)] [dir north] [turns turns]) + (if (empty? turns) + (displayln (format "part 1 (dist of final location): ~a" (loc-dist (car locs)))) + (let* ([turn (car turns)] + [rotation (car turn)] + [new-dir (* dir rotation)] + [dist (cdr turn)] + [loc (car locs)] + [new-loc (+ loc (* new-dir dist))]) + + (when (and + (not first-loc-visited-twice) + (report* locs loc new-loc (member new-loc locs))) + (set! first-loc-visited-twice new-loc) + (displayln (format "part 2 (dist of first twice-visited location): ~a" (loc-dist new-loc)))) + (loop (report (append (locs-between new-loc loc) (cdr locs))) new-dir (cdr turns)))))) + +(define-macro-cases turn + [(_ DIR DIST) + (with-pattern ([NEW-DIR (syntax-case #'DIR () + ["L" #'+i] + ["R" #'-i])]) + #'(cons NEW-DIR (string->number DIST)))] + [else #'(void)]) (provide turn) \ No newline at end of file