Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions list-reduction/josephus.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
(defn shout [counter nth coll]
(if (== (count coll) 1)
(defn shout
([nth coll] (let [to-kill (dec nth)] (shout to-kill to-kill coll)))
([counter to-kill coll]
(if (empty? (rest coll))
(first coll)
(shout
(rem (+ counter (count coll)) nth)
nth
(keep-indexed #(if(not= 0 (rem (+ %1 counter) nth)) %2) coll))))
(let [[next-counter next-coll]
(loop [current-idx counter
to-process coll
res []]
(if (empty? to-process)
[current-idx res]
(if (== current-idx to-kill)
(recur 0 (rest to-process) res)
(recur (unchecked-inc current-idx)
(rest to-process)
(conj res (first to-process))))))]
(recur next-counter to-kill next-coll)))))

(defn josephus [people nth]
(shout 0 nth (range 1 (inc people))))
(shout nth (range 1 (inc people))))

;(defn countdown [iterations]
; (dotimes [_ iterations]
Expand Down