Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Created November 4, 2013 17:09
Show Gist options
  • Save DeaconDesperado/7305875 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/7305875 to your computer and use it in GitHub Desktop.
Scala implementation of the legos problem, infinitely recursing!!!
import scala.collection.mutable._
object Legos {
val mod = 1000000007;
def t(width:Int):Int = {
if(width < 0) 0
if(width == 0) 1
t(width-1) + t(width-2) + t(width-3) + t(width-4)
}
def allNonSolid(width:Int,height:Int):Int = {
Math.pow(t(width).toFloat,height.toFloat).toInt
}
def overX(x:Int,height:Int,width:Int):Int = {
println(x)
permuteSolids(x,height) * allNonSolid(width-x,height)
}
def permuteSolids(height:Int,width:Int):Int = {
allNonSolid(width,height) - (1 to width).map( overX(_,height,width) ).sum
}
def printmod(a:Int){
println(a % mod)
}
def main(args: Array[String]) {
io.Source.stdin.getLines
.map(_.trim.split("\\s+").map(_.toInt)) // split and convert to ints
.collect { case Array(a, b) => permuteSolids(a, b) } // pass to f if there are two arguments
.foreach(printmod)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment