File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ (ns y2024.d10
2
+ (:require
3
+ [advent-of-code-clj.utils :as utils]
4
+ [medley.core :as medley]
5
+ [advent-of-code-clj.input :as input]))
6
+
7
+ (def test-input " 89010123
8
+ 78121874
9
+ 87430965
10
+ 96549874
11
+ 45678903
12
+ 32019012
13
+ 01329801
14
+ 10456732" )
15
+
16
+ (defn trailhead-paths [coord-map only-distinct? trailhead]
17
+ (loop [path-heads [trailhead]
18
+ endpoints []]
19
+ (let [npath-heads
20
+ (for [[y x :as node] path-heads
21
+ neighbour-node (utils/adjacent-hv y x)
22
+ :when (= (coord-map neighbour-node) (inc (coord-map node)))]
23
+ neighbour-node)
24
+ {np nil
25
+ ep 9 } (medley/collate-by (comp #{9 } coord-map) conj vector npath-heads)]
26
+ (if (empty? np)
27
+ (cond-> (into endpoints ep)
28
+ only-distinct? distinct)
29
+ (recur np (into endpoints ep))))))
30
+
31
+ (defn trailhead-scores [only-distinct matrix]
32
+ (let [coord-map (utils/coord-map-fixed (utils/emap (comp parse-long str) matrix))
33
+ trailheads (->> coord-map
34
+ (medley/filter-vals #{0 })
35
+ (map first))]
36
+ (for [trailhead trailheads]
37
+ (count (trailhead-paths coord-map only-distinct trailhead)))))
38
+
39
+ (= 36 (reduce + (trailhead-scores true (utils/text->matrix test-input))))
40
+
41
+ (= 531 (reduce + (trailhead-scores true (utils/text->matrix (input/get-input 2024 10 )))))
42
+
43
+ (= 81 (reduce + (trailhead-scores false (utils/text->matrix test-input))))
44
+
45
+ (= 1210 (reduce + (trailhead-scores false (utils/text->matrix (input/get-input 2024 10 )))))
You can’t perform that action at this time.
0 commit comments