class CGhostLeg(g:List[Int]) { val s:List[Int] = g.sort { (a,b) => a < b } var p:List[List[Boolean]] = List() def create:CGhostLeg = { def _calc(c:List[Int],r:Tuple2[List[Int],List[Boolean]]):Tuple2[List[Int],List[Boolean]] = c match { case List() => r case h::List() => (r._1+h,r._2) case h::t => h.compare(t.head) match { case p if p > 0 => _calc(c.slice(2).asInstanceOf[List[Int]],(r._1+c.apply(1)+h,r._2+true++(t match { case h::List() => List(); case _ => List(false) }))) case _ => _calc(t,(r._1+h,r._2+false)) } case _ => r } def _step(c:List[Int]):Unit = { val n = _calc(c,(List(),List())) p = n._2::p if (!n._1.zip(s).filter { d => d._1 != d._2 }.isEmpty) _step(n._1) } _step(g) this } def print:Unit = { def _join(d:String,l:List[String]):String = l.head + l.tail.foldLeft("") { (s,e) => s + d + e } println(_join(" ",s.map { e => e.toString })) p.foreach { l => println(_join("|",""::(l.map { e => e match { case true => "-"; case _ => " " } })+"")) } println(_join(" ",g.map { e => e.toString })) } } object GhostLeg { def main(args:Array[String]):Unit = { try { var g:List[Int] = args.length match { case 0 => List(3,5,2,4,0,1) case _ => args.toList.map { s => s.toInt } } (new CGhostLeg(g)).create.print } catch { case e => e.printStackTrace } } }