-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.clj
58 lines (51 loc) · 1.54 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
(ns advent-of-clojure.2021.02
(:require [advent-of-clojure.util :as util]
[clojure.string :as str]))
(def test-data
[[:forward 5]
[:down 5]
[:forward 8]
[:up 3]
[:down 8]
[:forward 2]])
(def data
(->> (util/read-input-lines 2021 02)
(map #(str/split % #" "))
(map (fn [[direction amount]]
[(keyword direction)
(Integer/parseInt amount)]))))
:input test-data
:output {:distance 15 :depth 10}
(defn follow-commands [commands]
(reduce
(fn [position [direction amount]]
(case direction
:forward (update position :distance + amount)
:down (update position :depth + amount)
:up (update position :depth - amount)))
{:distance 0 :depth 0}
commands))
;; A silly name but whatever ¯\(ツ)/¯
(defn distance-*-depth [{:keys [distance depth]}]
(* distance depth))
(def part-1
(->> data
follow-commands
distance-*-depth))
:input test-data
:output {:distance 15 :depth 60 :aim 10}
(defn follow-commands-2 [commands]
(reduce
(fn [{:keys [aim] :as position} [direction amount]]
(case direction
:down (update position :aim + amount)
:up (update position :aim - amount)
:forward (-> position
(update :distance + amount)
(update :depth + (* aim amount)))))
{:distance 0 :depth 0 :aim 0}
commands))
(def part-2
(->> data
follow-commands-2
distance-*-depth))