-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12_2.clj
62 lines (50 loc) · 1.36 KB
/
day12_2.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
56
57
58
59
60
61
62
(ns advent.day12-2
(:require [advent.core :as c]
[clojure.pprint :as pp]))
(defn parse [s]
;; split into command (single letter) and value
(let [command (keyword (str (first s)))
val (Integer/parseInt (apply str (rest s)))]
[command val]))
(defn read-input [file]
(map #(parse %) (c/read-lines file)))
(defn turn [waypoint cmd]
(let [[x y] waypoint
[c v] cmd]
(if (= 90 v)
(if (= :R c)
[y (* -1 x)]
[(* -1 y) x])
(if (= 270 v)
(if (= :R c)
[(* -1 y) x]
[y (* -1 x)])
;; 180
[(* -1 x)
(* -1 y)]))))
(defn move-ship [ship waypoint value]
;; ship + (value * waypoint)
(let [[x y] ship
[wx wy] waypoint]
[(+ x (* wx value))
(+ y (* wy value))]))
(defn move [[ship waypoint] cmd]
(let [[x y] ship
[wx wy] waypoint
[command value] cmd]
(case command
:E [ship [(+ wx value) wy]]
:W [ship [(- wx value) wy]]
:N [ship [wx (+ wy value)]]
:S [ship [wx (- wy value)]]
:L [ship (turn waypoint cmd)]
:R [ship (turn waypoint cmd)]
:F [(move-ship ship waypoint value) waypoint])))
(defn abs [x]
(if (< x 0)
(* -1 x) x))
(defn part2 [file]
;; [East North]
(let [[ship waypoint] (reduce move [[0 0] [10 1]] (read-input file))]
(let [[x y] ship]
(+ (abs x) (abs y)))))