init day 10

This commit is contained in:
2024-12-10 21:23:12 -05:00
parent c2f421cc03
commit a09033eaff
3 changed files with 112 additions and 0 deletions
+6
View File
@@ -27,6 +27,12 @@
col (range (count (get list-of-lists row)))]
[row col]))
(defn map-by-coords [arr-2d]
(->> arr-2d
get-coords
(map (juxt identity #(get (get arr-2d (first %)) (second %))))
(into {})))
(defn insert-at-idx [coll idx el]
(concat (take idx coll)
(list el)
+56
View File
@@ -0,0 +1,56 @@
(ns day10
(:require [core :as c]
[input-manager :refer [get-input]]))
(def input (update-vals (c/map-by-coords (get-input 10))
#(Character/getNumericValue %)))
(def trailheads (->> input
(filter #(= 0 (second %)))
(map first)))
(defn possible-steps [loc]
(let [c (input loc)]
(->> [(update loc 0 inc)
(update loc 0 dec)
(update loc 1 inc)
(update loc 1 dec)]
(filter #(= (input %) (inc c))))))
(defn get-score [trailhead]
(->> (loop [[loc & locs] [trailhead]
seen #{trailhead}]
(if (nil? loc)
seen
(let [steps (filter (comp not seen)
(possible-steps loc))]
(recur (into locs steps)
(into seen steps)))))
(filter #(= 9 (input %)))
count))
;; part 1
(->> trailheads
(map get-score)
(reduce +))
(defn get-trails [trailhead]
(->> (loop [[[loc path] & locs] [[trailhead []]]
trails #{}]
(cond
(nil? loc) trails
(= (input loc) 9)
(recur locs (conj trails path))
:else
(let [steps (map (juxt identity #(conj path %))
(possible-steps loc))]
(recur (into locs steps)
trails))))))
;; part 2
(->> trailheads
(map get-trails)
(map count)
(reduce +))