Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created October 8, 2018 14:02
Show Gist options
  • Save Centaur/e8adeab79a4098e516c0757d0ed064c3 to your computer and use it in GitHub Desktop.
Save Centaur/e8adeab79a4098e516c0757d0ed064c3 to your computer and use it in GitHub Desktop.
import scala.collection.AbstractIterator
def forward_from(n: Int, e: Int) = Iterator.continually(1 to n).flatten.slice(e - 1, e - 1 + n).toVector
def backward_from(n: Int, e: Int) = Iterator.continually(n.to(1, -1)).flatten.slice(n - e, n - e + n).toVector
def beiger_even(player_count: Int): Iterator[(Vector[Int], Vector[Int])] = new AbstractIterator[(Vector[Int], Vector[Int])] {
var next_round = 1
var flat = (1 to player_count).toVector
override def hasNext: Boolean = next_round <= player_count - 1
override def next(): (Vector[Int], Vector[Int]) = {
if (hasNext) {
val right_bottom = player_count >> 1
if (next_round > 1) {
flat = if ((next_round & 0x01) == 1) {
forward_from(player_count - 1, flat(right_bottom)) :+ player_count
} else {
(backward_from(player_count - 1, flat(right_bottom)) :+ player_count).reverse
}
}
next_round += 1
val (left, right) = flat.splitAt(right_bottom)
(left, right.reverse)
} else Iterator.empty.next()
}
}
def replace(a: Vector[Int], e: Int, v: Int) = a.map {
case `e` => v
case other => other
}
def beiger(player_count: Int) = if ((player_count & 0x01) == 1)
beiger_even(player_count + 1).map {
case (l, r) => (replace(l, player_count + 1, 0), replace(r, player_count + 1, 0))
} else beiger_even(player_count)
for {
(l, r) <- beiger(8)
i <- l.indices
} {
println(s"${l(i)}-${r(i)}")
}
for {
(l, r) <- beiger(7)
i <- l.indices
} {
println(s"${l(i)}-${r(i)}")
}
for {
(l, r) <- beiger(5)
i <- l.indices if l(i) != 0 && r(i) != 0
} {
println(s"${l(i)}-${r(i)}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment