Skip to content

Instantly share code, notes, and snippets.

@honzabrecka
Last active December 2, 2017 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzabrecka/315ce6b3838c01ab97ba0c4a4df12410 to your computer and use it in GitHub Desktop.
Save honzabrecka/315ce6b3838c01ab97ba0c4a4df12410 to your computer and use it in GitHub Desktop.
(defn shift
[[h & t]]
(concat t [h]))
(defn apply-times
[f n v]
(if (= n 0)
v
(recur f (dec n) (f v))))
; part 1
(let [col "1111"]
(transduce (comp (filter (partial apply =))
(map first)
(map int))
+
(map vector col (shift col))))
; part 2
(let [col "1212"
c (count (seq col))]
(transduce (comp (filter (partial apply =))
(map first)
(map int))
+
(map vector col (apply-times shift (/ c 2) col))))
(let [sheet [[5 1 9 5]
[7 5 3]
[2 4 6 8]]]
(transduce (comp (map sort)
(map (juxt last first))
(map (partial apply -)))
+
sheet))
(comment
; or juxt with min/max
(let [sheet [[5 1 9 5]
[7 5 3]
[2 4 6 8]]]
(transduce (comp (map (juxt (partial apply max)
(partial apply min)))
(map (partial apply -)))
+
sheet)))
(let [sheet [[5 9 2 8]
[9 4 7 3]
[3 8 6 5]]]
(transduce (comp (map (fn [col]
(for [a col
b col
:when (and (not= a b)
(zero? (mod a b)))]
[a b])))
(map first)
(map (partial apply /)))
+
sheet))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment