Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created June 18, 2014 00:02
Show Gist options
  • Save krishnanraman/107b5db2269f5032d1a2 to your computer and use it in GitHub Desktop.
Save krishnanraman/107b5db2269f5032d1a2 to your computer and use it in GitHub Desktop.
// See https://news.ycombinator.com/item?id=7856135
object zsb extends App {
type T = (Int,Int,Int)
def ok(zsb:T) = { val (z,s,b) = zsb; z>=0 && s>=0 && b>=0 }
def parse(s:String) = { val arr = s.split(",").map(s=> s.toInt); (arr(0), arr(1), arr(2)) }
def buyout(zsb:T, set:Set[T]):Set[T] = {
val (z,s,b) = zsb // z zombie, s startup, b bigcorp
val ret = Set((z-1, s-1, b+1), (z-1,s+1,b-1), (z+1,s-1,b-1))
.filter(ok)
.diff(set)
if (ret.size > 0) set ++ ret else set
}
def loop(zsb:T, set:Set[T]):Unit = {
val newset = buyout(zsb,set)
if ( newset != set) {
//println(newset.size)
//newset.foreach(println)
newset.foreach { e => loop(e,newset) }
} //else {
println(newset.size)
newset.foreach(println)
//}
}
loop(parse(args(0)), Set[T]())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment