day 10 WIP
This commit is contained in:
parent
93266bfca3
commit
5ef8f4af1d
47
src/day10.clj
Normal file
47
src/day10.clj
Normal file
@ -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)))
|
||||||
@ -67,12 +67,33 @@ ijkl")
|
|||||||
(< x (w a))
|
(< x (w a))
|
||||||
(< y (h a)))))
|
(< y (h a)))))
|
||||||
|
|
||||||
(test #'read-mapped-area)
|
|
||||||
(test #'h)
|
|
||||||
(test #'w)
|
|
||||||
(test #'mapped?)
|
|
||||||
|
|
||||||
(defn mapped-area-indexed-seq [a]
|
(defn mapped-area-indexed-seq [a]
|
||||||
(for [x (range (w a))
|
(for [x (range (w a))
|
||||||
y (range (h a))]
|
y (range (h a))]
|
||||||
(vector [x y] (get-in a [x y]))))
|
(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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user