This commit is contained in:
Adam Jeniski 2024-12-05 10:44:51 -05:00
parent d5e9f85dc8
commit 1bd800472d
2 changed files with 21 additions and 29 deletions

View File

@ -22,14 +22,15 @@
(println x)
x)
(defn get-coords
"returns a lazy seq representing list of list x, y tuples"
[list-of-lists]
(->> list-of-lists count range
(map #(->> % (get list-of-lists) count range))
(map-indexed (fn [row cols]
(map #(list row %) cols)))
(mapcat identity)))
(defn get-coords [list-of-lists]
(for [row (range (count list-of-lists))
col (range (count (get list-of-lists row)))]
[row col]))
(defn insert-vec [v idx el]
(into [] (concat (take idx v)
(list el)
(drop idx v))))
(defn bool->binary [condition]
(if condition 1 0))

View File

@ -18,29 +18,22 @@
(get v (int (/ (count v) 2))))
(defn ordered? [coll]
(reduce (fn [acc p]
(if p
(if (some acc (orderings p))
(reduced false)
(conj acc p))
(reduced true)))
(reduce (fn [acc el]
(if
(some acc (orderings el)) (reduced false)
(conj acc el)))
#{}
coll))
(defn order [coll]
(reduce (fn [acc p]
(if p
(if-let [idxs (->> (orderings p)
(filter #(contains? (set acc) %))
(map #(.indexOf acc %))
(filter #(not= % -1))
not-empty)]
(let [m (apply min idxs)]
(into [] (concat (take m acc)
(list p)
(drop m acc))))
(conj acc p))
(reduced acc)))
(c/insert-vec acc (apply min idxs) p)
(conj acc p)))
[]
coll))
@ -53,8 +46,6 @@
;; part 2
(->> pages
(filter #(not (ordered? %)))
(map order)
(map get-middle)
(map (comp get-middle order))
(reduce +))