Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2016 07:57
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 anonymous/fb2a841f3029afc93041b74d126a639b to your computer and use it in GitHub Desktop.
Save anonymous/fb2a841f3029afc93041b74d126a639b to your computer and use it in GitHub Desktop.
(def input1 [[3 1 2 3]
[3 2 3 1]
[4 2 3 4 5]])
(def input2 [[2 1 2]
[5 2 8]])
(def input3 [[7 11 2 2 4 8 2 2]
[3 0 11 8]
[5 11 8 10 3 11]
[5 9 2 5 0 3]
[7 4 8 2 8 1 0 5]
[3 6 8 9]
[4 2 11 3 3]])
(def input4 [[12 23 15 2 8 20 21 3 23 3 27 20 0]
[21 14 8 20 10 0 23 3 24 23 0 19 14 12 10 9 12 12 11 6 27 5]
[8 18 27 10 11 22 29 23 14]
[13 7 14 1 9 14 16 12 0 10 13 19 16 17]
[24 25 21 4 6 19 1 3 26 11 22 28 14 14 27 7 20 8 7 4 1 8 10 18 21]
[13 20 26 22 6 5 6 23 26 2 21 16 26 24]
[6 7 17 2 22 23 21]
[23 14 22 28 10 23 7 21 3 20 24 23 8 8 21 13 15 6 9 17 27 17 13 14]
[23 13 1 15 5 16 7 26 22 29 17 3 14 16 16 18 6 10 3 14 10 17 27 25]
[25 28 5 21 8 10 27 21 23 28 7 20 6 6 9 29 27 26 24 3 12 10 21 10 12 17]
[26 22 26 13 10 19 3 15 2 3 25 29 25 19 19 24 1 26 22 10 17 19 28 11 22 2 13]
[8 4 25 15 20 9 11 3 19]
[24 29 4 17 2 0 8 19 11 28 13 4 16 5 15 25 16 5 6 1 0 19 7 4 6]
[16 25 15 17 20 27 1 11 1 18 14 23 27 25 26 17 1]])
(defn update-drivers [meeting-drivers alway-gossip?]
(let [new-gossip (->> meeting-drivers
(map :gossip)
(reduce clojure.set/union))
gossiping? (some #(not= new-gossip (:gossip %))
meeting-drivers)]
(map
(fn [driver]
(-> driver
(update :schedule #(cond-> %
(or (not gossiping?)
always-gossip?) rest))
(assoc :gossip new-gossip)))
meeting-drivers)))
(defn start-gossip [input max-steps alway-gossip?]
(->> {:drivers (map-indexed (fn [idx schedule]
{:driver-id idx
:gossip #{idx}
:schedule (cycle schedule)})
input)
:step 0}
(iterate
(fn [{:keys [drivers step]}]
{:drivers (->> drivers
(group-by (comp first :schedule))
(mapcat
(fn [[meeting-point meeting-drivers]]
(update-drivers meeting-drivers alway-gossip?))))
:step (inc step)}))
(drop-while
(fn [{:keys [drivers step]}]
(and (< step max-steps)
(not= 1 (count (into #{} (map :gossip drivers)))))))
(first)
(:step)))
(defn all-knowing-drivers? [input max-steps alway-gossip?]
(let [res (start-gossip input max-steps alway-gossip?)]
(if (not= max-steps res)
res
:never)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment