diff --git a/12.rkt b/12.rkt index dbff465..d5e05fa 100644 --- a/12.rkt +++ b/12.rkt @@ -6,50 +6,35 @@ ;; helper functions here -(define (d->r deg) - (* (/ deg 180) pi)) +(define d->r degrees->radians) (define (part1 input) - (define-values [dir x y] - (for/fold ([dir 0] [x 0] [y 0]) ([ins (in-list input)]) - (match-define (cons ltr num) ins) - (match ltr - ["N" (values dir x (+ y num))] - ["S" (values dir x (- y num))] - ["E" (values dir (+ x num) y)] - ["W" (values dir (- x num) y)] - ["L" (values (+ dir (d->r num)) x y)] - ["R" (values (- dir (d->r num)) x y)] - ["F" (values dir (+ x (* num (cos dir))) (+ y (* num (sin dir))))]))) - (inexact->exact (floor (+ (abs x) (abs y))))) - -(define (part2 input) - (define (rot x y d) - (values - (- (* x (cos d)) (* y (sin d))) - (+ (* x (sin d)) (* y (cos d))))) - (define-values [sx sy wx wy] - (for/fold ([sx 0] [sy 0] [wx 10] [wy 1]) ([ins (in-list input)]) + (define-values [dir s] + (for/fold ([dir 0] [s 0]) ([ins (in-list input)]) (match-define (cons ltr num) ins) (match ltr - ["N" (values sx sy wx (+ wy num))] - ["S" (values sx sy wx (- wy num))] - ["E" (values sx sy (+ wx num) wy)] - ["W" (values sx sy (- wx num) wy)] - ["L" - (define-values [nwx nwy] (rot wx wy (d->r num))) - (values sx sy nwx nwy)] - ["R" - (define-values [nwx nwy] (rot wx wy (d->r (- num)))) - (values sx sy nwx nwy)] - ["F" - (values (+ sx (* num wx)) (+ sy (* num wy)) wx wy)]))) - (inexact->exact (floor (+ (abs sx) (abs sy))))) + ["N" (values dir (+ s (* +i num)))] + ["S" (values dir (- s (* +i num)))] + ["E" (values dir (+ s num))] + ["W" (values dir (- s num))] + ["L" (values (+ dir (d->r num)) s)] + ["R" (values (- dir (d->r num)) s)] + ["F" (values dir (+ s (make-polar num dir)))]))) + (inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s)))))) -(module+ test - (require rackunit) - ;; tests here - (displayln "no tests :(")) +(define (part2 input) + (define-values [s w] + (for/fold ([s 0] [w 10+1i]) ([ins (in-list input)]) + (match-define (cons ltr num) ins) + (match ltr + ["N" (values s (+ w (* +i num)))] + ["S" (values s (- w (* +i num)))] + ["E" (values s (+ w num))] + ["W" (values s (- w num))] + ["L" (values s (* w (make-polar 1 (d->r num))))] + ["R" (values s (* w (make-polar 1 (d->r (- num)))))] + ["F" (values (+ s (* w num)) w)]))) + (inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s)))))) (module+ main (define input