Last active
August 29, 2015 14:21
-
-
Save jsvensson/b9b1f42ce3d8e89261bc to your computer and use it in GitHub Desktop.
Josephus solution in Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Josephus { | |
void execute(size = 41, steps = 3, survivors = 2) { | |
def prisoners = (1..size).toArray() | |
while(prisoners.length > survivors) { | |
(steps-1).times { | |
prisoners = prisoners.tail() + prisoners.head() | |
} | |
// Execute first prisoner | |
prisoners = prisoners.tail() | |
} | |
println("${prisoners.length} prisoners left") | |
println(prisoners) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment