-
Notifications
You must be signed in to change notification settings - Fork 1
/
2020-12-rain-risk.clj
37 lines (33 loc) · 1.14 KB
/
2020-12-rain-risk.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
(defn parse [ln]
(let [[_ c n] (re-find #"(\w){1}([0-9]+)" ln)]
[(first c) (Integer. n)]))
(defn rotate [x y deg]
(case deg
90 [(* y -1) x]
180 [(* x -1) (* y -1)]
270 [y (* x -1)]))
(defn solve-a
([[ax ay _ _]] (+ (abs ax) (abs ay)))
([[ax ay bx by] [cd vl]]
(case cd
\N [ax (- ay vl) bx by]
\E [(+ ax vl) ay bx by]
\S [ax (+ ay vl) bx by]
\W [(- ax vl) ay bx by]
\L (let [[rx ry] (rotate bx by (- 360 vl))] [ax ay rx ry])
\R (let [[rx ry] (rotate bx by vl)] [ax ay rx ry])
\F [(+ ax (* vl bx)) (+ ay (* vl by)) bx by])))
(defn solve-b
([[ax ay _ _]] (+ (abs ax) (abs ay)))
([[ax ay bx by] [cd vl]]
(case cd
\N [ax ay bx (- by vl)]
\E [ax ay (+ bx vl) by]
\S [ax ay bx (+ by vl)]
\W [ax ay (- bx vl) by]
\L (let [[rx ry] (rotate bx by (- 360 vl))] [ax ay rx ry])
\R (let [[rx ry] (rotate bx by vl)] [ax ay rx ry])
\F [(+ ax (* vl bx)) (+ ay (* vl by)) bx by])))
(let [in (line-seq (java.io.BufferedReader. *in*))]
(println "Part A:" (transduce (map parse) solve-a [0 0 1 0] in))
(println "Part B:" (transduce (map parse) solve-b [0 0 10 -1] in)))