-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
46 lines (39 loc) · 1.25 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
(ns _2021.two.core
(:require [util :refer [input]]
[clojure.string :as string]))
(comment
(input "example.txt")
(input "input.txt"))
(comment
;; part 1
(->> (input "input.txt")
(map #(string/split % #" "))
(reduce (fn [acc [cmd val]]
(cond
(= cmd "forward")
(update acc :x + (read-string val))
(= cmd "up")
(update acc :y - (read-string val))
(= cmd "down")
(update acc :y + (read-string val))))
{:x 0 :y 0})
((fn [{:keys [x y]}] (* x y)))))
(comment
;; part 2
(->> (input "input.txt")
(map #(string/split % #" "))
(reduce (fn [acc [cmd val]]
(let [v (read-string val)
aim (:aim acc)
]
(cond
(= cmd "forward")
(-> acc
(update :x + v)
(update :y + (* aim v)))
(= cmd "up")
(update acc :aim - v)
(= cmd "down")
(update acc :aim + v))))
{:x 0 :y 0 :aim 0})
((fn [{:keys [x y]}] (* x y)))))