2020-12-12 05:23:37 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 12
|
|
|
|
|
|
|
|
;; helper functions here
|
|
|
|
|
2020-12-12 05:54:22 +00:00
|
|
|
(define d->r degrees->radians)
|
2020-12-12 06:02:03 +00:00
|
|
|
(define (complex-manhat s)
|
|
|
|
(inexact->exact (round (+ (abs (real-part s)) (abs (imag-part s))))))
|
2020-12-12 05:23:37 +00:00
|
|
|
|
|
|
|
(define (part1 input)
|
2020-12-12 06:02:03 +00:00
|
|
|
(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)))])))
|
2020-12-12 05:23:37 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
2020-12-12 06:02:03 +00:00
|
|
|
(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)])))
|
2020-12-12 05:23:37 +00:00
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input
|
|
|
|
(for/list ([line (in-list (file->lines "inputs/12"))])
|
|
|
|
(match line
|
|
|
|
[(pregexp #px"^(.)([0-9]+)$" (list _ dir (app string->number num)))
|
|
|
|
(cons dir num)])))
|
|
|
|
;; part 1
|
|
|
|
(answer 12 1 (part1 input))
|
|
|
|
|
|
|
|
;; part 2
|
|
|
|
(answer 12 2 (part2 input))
|
|
|
|
|
|
|
|
(displayln "meow"))
|