-
Notifications
You must be signed in to change notification settings - Fork 3
/
day01.clj
53 lines (43 loc) · 1.15 KB
/
day01.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
(ns day01
(:require aoc))
(defn parse-instruction [instr]
(let [rot (first instr)
amt (first (aoc/integers instr))]
{:rot (case rot \L :left \R :right)
:amt amt}))
(defn rotate [[x y] rot]
(case rot
:left [y (- x)]
:right [(- y) x]))
(defn step [state]
(let [{:keys [pos dir hq seen]} state
new-pos (aoc/pt+ pos dir)]
{:pos new-pos
:dir dir
:hq (if (and (not hq) (seen new-pos))
new-pos
hq)
:seen (conj seen new-pos)}))
(defn move [state amt]
(-> (iterate step state)
(nth amt)))
(defn traverse [instructions]
(let [state {:pos [0 0]
:dir [0 -1]
:seen #{[0 0]}
:hq false}]
(reduce
(fn [state {:keys [rot amt]}]
(let [new-dir (rotate (:dir state) rot)]
(-> state
(assoc :dir new-dir)
(move amt))))
state
instructions)))
(defn solve [input]
(let [instructions (->> (aoc/read-input-line input :words)
(map parse-instruction))
solution (traverse instructions)]
(map aoc/manhattan
((juxt :pos :hq) solution))))
(solve 1)