Skip to content

Instantly share code, notes, and snippets.

@yassu
Last active December 23, 2018 05:40
Show Gist options
  • Save yassu/8daf1b31d9233fbded16fb9c3ebb8886 to your computer and use it in GitHub Desktop.
Save yassu/8daf1b31d9233fbded16fb9c3ebb8886 to your computer and use it in GitHub Desktop.
compute original generic_like_pool by recursive
import scala.collection.mutable
object GenericLikePool {
def newDiag(beforeDiag: Seq[Int], num: Int, m: Int): Seq[Int] = {
var s = -1
(0 until beforeDiag.size + 1).map( j => {
s = if(j == 0) num else (beforeDiag(j - 1) - s + m) % m
s
}
)
}
def getPuzzles(headSize: Int, r: Int): Seq[Seq[Seq[Int]]] = {
def puzzles(
diags: Seq[Seq[Int]], restList: Seq[Int], m: Int)
: Seq[Seq[Seq[Int]]] = {
val lastDiag = diags.lastOption match {
case Some(d) => d
case None => Seq[Int]()
}
restList
.distinct
.map(n => newDiag(lastDiag, n, m))
.filter(d => subsetSeq(restList, d))
.map(d => (diags :+ d, removeSeq(restList, d)))
.map(t =>
if(t._2.size == 0) Seq(t._1)
else puzzles(t._1, t._2, m))
.flatten
}
val numberOfAllPos = headSize * (headSize + 1) / 2
val m = numberOfAllPos / r // mod
val allNumbers = Seq.fill(r)(0 until m)
.flatten.map(j => j % m).sortBy(k => k).toList
puzzles(Seq(), allNumbers, m)
}
def removeSeq[T](l1: Seq[T], l2: Seq[T]): Seq[T] = {
val a1 = mutable.ArrayBuffer(l1: _*)
val a2 = mutable.ArrayBuffer(l2: _*)
(a1 -- a2).toSeq
}
// s1 in s2
def subsetSeq[T](s1: Seq[T], s2: Seq[T]): Boolean =
removeSeq(s1, s2).size == s1.size - s2.size
def main(args: Array[String]) = {
val maxHeadSize = 10
(1 to maxHeadSize).map(n =>
(1 to n * (n + 1) / 2).filter((n * (n + 1) / 2) % _ == 0).map(m => (n, m))).flatten
.foreach(t => {
val (headSize, r) = t
val m = headSize * (headSize + 1) / (2 * r)
println(s"(headSize, repeatNumber, mod) = ($headSize, $r, $m)")
val puzzles = getPuzzles(headSize, r)
println("res:")
puzzles.foreach(println)
println(s"count: ${puzzles.size}")
println("=" * 80)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment