mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 07:03:19 -09:00
golf
This commit is contained in:
parent
d5e9f85dc8
commit
1bd800472d
@ -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))
|
||||
|
@ -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)))
|
||||
(if-let [idxs (->> (orderings p)
|
||||
(filter #(contains? (set acc) %))
|
||||
(map #(.indexOf acc %))
|
||||
(filter #(not= % -1))
|
||||
not-empty)]
|
||||
(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 +))
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user