-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday12.clj
55 lines (45 loc) · 1.54 KB
/
day12.clj
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns advent-2020-clojure.day12
(:require [clojure.string :as str]
[advent-2020-clojure.utils :as utils]))
(defn new-ship [initial-waypoint]
{:ship [0 0] :waypoint initial-waypoint})
(def dir-amounts {:north [0 -1]
:south [0 1]
:east [1 0]
:west [-1 0]})
(defn move [state mover target amt]
(let [move-by (mapv * target [amt amt])]
(update state mover (partial mapv + move-by))))
(defn slide [state mover dir amt]
(move state mover (dir-amounts dir) amt))
(defn follow-waypoint [state amt]
(move state :ship (state :waypoint) amt))
(defn rotate-waypoint [state degrees]
(let [times (-> degrees (mod 360) (quot 90))
rotate90 (fn [[x y]] [(- y) x])]
(-> (iterate
(fn [s] (update s :waypoint rotate90))
state)
(nth times))))
(defn next-state [state mover line]
(let [op (first line)
amt (-> (subs line 1) Integer/parseInt)]
(case op
\N (slide state mover :north amt)
\S (slide state mover :south amt)
\E (slide state mover :east amt)
\W (slide state mover :west amt)
\L (rotate-waypoint state (- amt))
\R (rotate-waypoint state amt)
\F (follow-waypoint state amt))))
(defn solve [initial-waypoint mover input]
(->> (reduce #(next-state %1 mover %2)
(new-ship initial-waypoint)
(str/split-lines input))
:ship
(mapv utils/abs)
(apply +)))
(defn part1 [input]
(solve [1 0] :ship input))
(defn part2 [input]
(solve [10 -1] :waypoint input))