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

View File

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