-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday12.lisp
More file actions
37 lines (31 loc) · 1.32 KB
/
day12.lisp
File metadata and controls
37 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(defpackage :aoc/2020/12 #.cl-user::*aoc-use*)
(in-package :aoc/2020/12)
(defun parse-instruction (string)
(cons (char string 0) (parse-integer string :start 1)))
(defun parse-instructions (data)
(mapcar #'parse-instruction data))
(defun part1 (instructions)
(loop with pos = 0 with dir = #c(1 0) for (ch . n) in instructions do
(ecase ch
(#\N (incf pos (* n #c(0 1))))
(#\S (incf pos (* n #c(0 -1))))
(#\E (incf pos (* n #c(1 0))))
(#\W (incf pos (* n #c(-1 0))))
(#\L (mulf dir (expt #c(0 1) (/ n 90))))
(#\R (mulf dir (expt #c(0 -1) (/ n 90))))
(#\F (incf pos (* n dir))))
finally (return (manhattan-distance pos 0))))
(defun part2 (instructions)
(loop with pos = 0 with dir = #c(10 1) for (ch . n) in instructions do
(ecase ch
(#\N (incf dir (* n #c(0 1))))
(#\S (incf dir (* n #c(0 -1))))
(#\E (incf dir (* n #c(1 0))))
(#\W (incf dir (* n #c(-1 0))))
(#\L (mulf dir (expt #c(0 1) (/ n 90))))
(#\R (mulf dir (expt #c(0 -1) (/ n 90))))
(#\F (incf pos (* n dir))))
finally (return (manhattan-distance pos 0))))
(define-solution (2020 12) (instructions parse-instructions)
(values (part1 instructions) (part2 instructions)))
(define-test (2020 12) (521 22848))