day 12: use #:result
This commit is contained in:
parent
f8f7363dba
commit
a8825b52cb
10
12.rkt
10
12.rkt
|
@ -7,10 +7,11 @@
|
||||||
;; helper functions here
|
;; helper functions here
|
||||||
|
|
||||||
(define d->r degrees->radians)
|
(define d->r degrees->radians)
|
||||||
|
(define (complex-manhat s)
|
||||||
|
(inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s))))))
|
||||||
|
|
||||||
(define (part1 input)
|
(define (part1 input)
|
||||||
(define-values [dir s]
|
(for/fold ([dir 0] [s 0] #:result (complex-manhat s)) ([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 (+ s (* +i num)))]
|
["N" (values dir (+ s (* +i num)))]
|
||||||
|
@ -20,11 +21,9 @@
|
||||||
["L" (values (+ dir (d->r num)) s)]
|
["L" (values (+ dir (d->r num)) s)]
|
||||||
["R" (values (- dir (d->r num)) s)]
|
["R" (values (- dir (d->r num)) s)]
|
||||||
["F" (values dir (+ s (make-polar num dir)))])))
|
["F" (values dir (+ s (make-polar num dir)))])))
|
||||||
(inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s))))))
|
|
||||||
|
|
||||||
(define (part2 input)
|
(define (part2 input)
|
||||||
(define-values [s w]
|
(for/fold ([s 0] [w 10+1i] #:result (complex-manhat s)) ([ins (in-list input)])
|
||||||
(for/fold ([s 0] [w 10+1i]) ([ins (in-list input)])
|
|
||||||
(match-define (cons ltr num) ins)
|
(match-define (cons ltr num) ins)
|
||||||
(match ltr
|
(match ltr
|
||||||
["N" (values s (+ w (* +i num)))]
|
["N" (values s (+ w (* +i num)))]
|
||||||
|
@ -34,7 +33,6 @@
|
||||||
["L" (values s (* w (make-polar 1 (d->r num))))]
|
["L" (values s (* w (make-polar 1 (d->r num))))]
|
||||||
["R" (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)])))
|
["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