diff --git a/src/day10.clj b/src/day10.clj new file mode 100644 index 0000000..35dd8c1 --- /dev/null +++ b/src/day10.clj @@ -0,0 +1,47 @@ +(ns day10 + (:require [mapped-area] + [helpers :refer [get-input]])) + +(def test-map + (mapped-area/parse-elements + (mapped-area/read-mapped-area +"89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732"))) + +(defn trailheads [a] + (reduce + (fn [coll [coord value]] + (if (= value 0) + (conj coll coord) + coll)) + [] + (mapped-area/mapped-area-indexed-seq a))) + +(defn step [a [x y]] + (let [c (get-in a [x y]) + even-gradual-uphill? + (fn [[x' y']] + (let [c' (get-in a [x' y'])] + (= 1 (- c' c))))] + (->> + [[(inc x) y] [(dec x) y] + [x (inc y)] [x (dec y)]] + (filter (partial mapped-area/mapped? a)) + (filter even-gradual-uphill?)))) + +(defn walk [a trailhead] + (loop [positions [trailhead]] + (println positions) + (if (== 9 (get-in a (first positions))) + (count positions) + (recur (mapcat (partial step a) (set positions)))))) + + +(reduce + + (map (partial walk test-map) (trailheads test-map))) diff --git a/src/mapped_area.clj b/src/mapped_area.clj index 49bb087..09a7e02 100644 --- a/src/mapped_area.clj +++ b/src/mapped_area.clj @@ -67,12 +67,33 @@ ijkl") (< x (w a)) (< y (h a))))) -(test #'read-mapped-area) -(test #'h) -(test #'w) -(test #'mapped?) - (defn mapped-area-indexed-seq [a] (for [x (range (w a)) y (range (h a))] (vector [x y] (get-in a [x y])))) + +(def test-map-num +"1234 +5678") + +(defn parse-elements + {:test + #(do + (let [a (parse-elements + (read-mapped-area test-map-num))] + (assert (number? (get-in a [0 0]))) + (assert (== 5 (get-in a [0 0]))) + (assert (== 1 (get-in a [0 1])))))} + ([a] (parse-elements a (comp parse-long str))) + ([a parse-fn] + (reduce + (fn [coll [coord value]] + (assoc-in coll coord (parse-fn value))) + [[]] + (mapped-area-indexed-seq a)))) + +(test #'read-mapped-area) +(test #'h) +(test #'w) +(test #'mapped?) +(test #'parse-elements)