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