day 12: rewrite using complex numbers
This commit is contained in:
parent
a612240e50
commit
f8f7363dba
63
12.rkt
63
12.rkt
|
@ -6,50 +6,35 @@
|
||||||
|
|
||||||
;; helper functions here
|
;; helper functions here
|
||||||
|
|
||||||
(define (d->r deg)
|
(define d->r degrees->radians)
|
||||||
(* (/ deg 180) pi))
|
|
||||||
|
|
||||||
(define (part1 input)
|
(define (part1 input)
|
||||||
(define-values [dir x y]
|
(define-values [dir s]
|
||||||
(for/fold ([dir 0] [x 0] [y 0]) ([ins (in-list input)])
|
(for/fold ([dir 0] [s 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)])
|
|
||||||
(match-define (cons ltr num) ins)
|
(match-define (cons ltr num) ins)
|
||||||
(match ltr
|
(match ltr
|
||||||
["N" (values sx sy wx (+ wy num))]
|
["N" (values dir (+ s (* +i num)))]
|
||||||
["S" (values sx sy wx (- wy num))]
|
["S" (values dir (- s (* +i num)))]
|
||||||
["E" (values sx sy (+ wx num) wy)]
|
["E" (values dir (+ s num))]
|
||||||
["W" (values sx sy (- wx num) wy)]
|
["W" (values dir (- s num))]
|
||||||
["L"
|
["L" (values (+ dir (d->r num)) s)]
|
||||||
(define-values [nwx nwy] (rot wx wy (d->r num)))
|
["R" (values (- dir (d->r num)) s)]
|
||||||
(values sx sy nwx nwy)]
|
["F" (values dir (+ s (make-polar num dir)))])))
|
||||||
["R"
|
(inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s))))))
|
||||||
(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)))))
|
|
||||||
|
|
||||||
(module+ test
|
(define (part2 input)
|
||||||
(require rackunit)
|
(define-values [s w]
|
||||||
;; tests here
|
(for/fold ([s 0] [w 10+1i]) ([ins (in-list input)])
|
||||||
(displayln "no tests :("))
|
(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
|
(module+ main
|
||||||
(define input
|
(define input
|
||||||
|
|
Loading…
Reference in New Issue