-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_02.clj
executable file
·47 lines (34 loc) · 1.25 KB
/
day_02.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
#!/usr/bin/env bb
(require '[clojure.string :as str])
(def input (str/split-lines (slurp "./input/day_02.txt")))
;; ------------------------------------ part 1
(defn pos-updater
[key fu]
(fn [position value]
(assoc position key (fu (key position) value))))
(def forward (pos-updater :horizontal +))
(def down (pos-updater :depth +))
(def up (pos-updater :depth -))
(defn move
[input]
(let [computed (reduce (fn [position row]
(let [[cmd val] (str/split row #" ")
k (resolve (symbol cmd))
v (Integer/parseInt val)]
(apply k [position v])))
{:depth 0 :horizontal 0 :aim 0}
input)]
(* (:depth computed) (:horizontal computed))))
(println "sol 1:" (move input))
;; ------------------------------------ part 2
#_{:clj-kondo/ignore [:redefined-var]}
(defn forward
[position value]
(assoc position
:horizontal (+ (:horizontal position) value)
:depth (+ (* value (:aim position)) (:depth position))))
#_{:clj-kondo/ignore [:redefined-var]}
(def down (pos-updater :aim +))
#_{:clj-kondo/ignore [:redefined-var]}
(def up (pos-updater :aim -))
(println "sol 2:" (move input))