day 12: use #:result

This commit is contained in:
xenia 2020-12-12 01:02:03 -05:00
parent f8f7363dba
commit a8825b52cb
1 changed files with 22 additions and 24 deletions

46
12.rkt
View File

@ -7,34 +7,32 @@
;; helper functions here
(define d->r degrees->radians)
(define (complex-manhat s)
(inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s))))))
(define (part1 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 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))))))
(for/fold ([dir 0] [s 0] #:result (complex-manhat s)) ([ins (in-list input)])
(match-define (cons ltr num) ins)
(match ltr
["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)))])))
(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))))))
(for/fold ([s 0] [w 10+1i] #:result (complex-manhat s)) ([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)])))
(module+ main
(define input