-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
143 lines (119 loc) · 3.82 KB
/
core.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(ns _2020.twelve.core
(:require [util :refer [input]]))
(defn parse [f]
(->> (input f)
(map (fn [line]
(let [[_ letter num] (->> (re-seq #"([N|S|E|W|F|L|R]{1})(\d+)" line)
first)]
{:letter letter :num (read-string num)})))))
(comment
(parse "example.txt")
(parse "input.txt")
)
(def commands-part-1
{"N" (fn [{:keys [position facing]} num]
{:position {:x (:x position)
:y (+ (:y position) num)}
:facing facing})
"S" (fn [{:keys [position facing]} num]
{:position {:x (:x position)
:y (- (:y position) num)}
:facing facing})
"E" (fn [{:keys [position facing]} num]
{:position {:x (+ (:x position) num)
:y (:y position)}
:facing facing})
"W" (fn [{:keys [position facing]} num]
{:position {:x (- (:x position) num)
:y (:y position)}
:facing facing})
"F" (fn [{:keys [facing] :as agg} num]
{:position
(:position (case (mod facing 360)
0 ((commands-part-1 "E") agg num)
90 ((commands-part-1 "S") agg num)
180 ((commands-part-1 "W") agg num)
270 ((commands-part-1 "N") agg num)))
:facing facing})
"L" (fn [{:keys [position facing]} num]
{:position position
:facing (- facing num)})
"R" (fn [{:keys [position facing]} num]
{:position position
:facing (+ facing num)})})
(comment
(mod 540 360)
)
(defn travel
([f commands] (travel f commands nil))
([f commands waypoint]
(->> (parse f)
(reduce
(fn [agg {:keys [letter num]}]
(println agg)
((commands letter) agg num))
{:position {:x 0 :y 0}
:waypoint waypoint
:facing 0}))))
(comment
(->> (travel "example.txt" commands-part-1)
:position
vals
(map #(Math/abs %))
(apply +))
(->> (travel "input.txt" commands-part-1)
:position
vals
(map #(Math/abs %))
(apply +))
)
(defn rotate-waypoint [num waypoint]
(case (mod num 360)
0 waypoint
90 {:x (:y waypoint)
:y (* -1 (:x waypoint))}
180 {:x (* -1 (:x waypoint))
:y (* -1 (:y waypoint))}
270 {:x (* -1 (:y waypoint))
:y (:x waypoint)}))
(def commands-part-2
{"N" (fn [{:keys [position waypoint]} num]
{:waypoint {:x (:x waypoint)
:y (+ (:y waypoint) num)}
:position position})
"S" (fn [{:keys [position waypoint]} num]
{:waypoint {:x (:x waypoint)
:y (- (:y waypoint) num)}
:position position})
"E" (fn [{:keys [position waypoint]} num]
{:waypoint {:x (+ (:x waypoint) num)
:y (:y waypoint)}
:position position})
"W" (fn [{:keys [position waypoint]} num]
{:waypoint {:x (- (:x waypoint) num)
:y (:y waypoint)}
:position position})
"F" (fn [{:keys [position waypoint]} num]
{:position
{:x (+ (:x position) (* num (:x waypoint)))
:y (+ (:y position) (* num (:y waypoint)))}
:waypoint waypoint})
"L" (fn [{:keys [position waypoint]} num]
{:position position
:waypoint (rotate-waypoint (- 360 num) waypoint)})
"R" (fn [{:keys [position waypoint]} num]
{:position position
:waypoint (rotate-waypoint num waypoint)})})
(comment
(->> (travel "example.txt" commands-part-2 {:x 10 :y 1})
:position
vals
(map #(Math/abs %))
(apply +))
(->> (travel "input.txt" commands-part-2 {:x 10 :y 1})
:position
vals
(map #(Math/abs %))
(apply +))
;; 51298 too low
)