Skip to content

Instantly share code, notes, and snippets.

@wookay
Created January 9, 2015 14:35
Show Gist options
  • Save wookay/7802066dd8160dbe8b5b to your computer and use it in GitHub Desktop.
Save wookay/7802066dd8160dbe8b5b to your computer and use it in GitHub Desktop.
prize.scala
class Prize {
var winners: List[Int] = List.empty
def play(list: List[Int]) : List[List[Int]] = {
var w = List[Int]()
var l = List[Int]()
for (idx <- 0 until list.length/2) {
val i = idx*2
if (list(i) < list(i+1)) {
w :+= list(i)
l :+= list(i+1)
} else {
w :+= list(i+1)
l :+= list(i)
}
}
return List(w, l)
}
def recur(list: List[List[Int]]) {
for (l <- list) {
var winner = play(l)
if (winner(0).length > 1) {
recur(winner)
} else {
winners :+= winner(0)(0)
winners :+= winner(1)(0)
}
}
}
def start(n: Int) {
for (vec <- Range(0,scala.math.pow(2,n).toInt).permutations) {
winners = List.empty
recur(List(vec.toList))
println(winners)
}
}
}
var prize = new Prize()
prize.start(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment