-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.clj
More file actions
81 lines (62 loc) · 1.26 KB
/
Copy pathday03.clj
File metadata and controls
81 lines (62 loc) · 1.26 KB
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
(ns advent-of-code.2017.day03)
(def puzzle 347991)
; See http://sprungcanary.net/2017/12/11/aoc-without-code/
; for solution without code
(def nmove-seq (mapcat #(identity [% %]) (iterate inc 1)))
(defn r
[m]
(mapv + m [1 0]))
(defn u
[m]
(mapv + m [0 1]))
(defn l
[m]
(mapv + m [-1 0]))
(defn d
[m]
(mapv + m [0 -1]))
(def m-seq
(mapcat #(repeat % %2) nmove-seq (cycle [r u l d])))
(def coord-seq
(reductions (fn [s m]
(m s))
[0 0]
m-seq))
(defn one
[puzzle]
(let [coord (first (drop (dec puzzle) coord-seq))]
(apply + (map #(Math/abs %) coord))))
(comment
(one puzzle))
; => 480
; Store value of coord in map
; grid: {[x1 y1] value}
(def start-grid
{[0 0] 1})
(defn n-coords
[c]
[(-> c r)
(-> c r u)
(-> c u)
(-> c u l)
(-> c l)
(-> c l d)
(-> c d)
(-> c d r)])
(defn add-neighboors
[grid coord]
(let [cs (n-coords coord)
ns (filter identity (map grid cs))]
(apply + ns)))
(defn two
[puzzle]
(reduce (fn [g c]
(let [ng (assoc g c (add-neighboors g c))]
(if (> (ng c) puzzle)
(reduced (ng c))
ng)))
start-grid
(drop 1 coord-seq)))
(comment
(two puzzle))
; => 349975