Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@betehess
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betehess/9326471 to your computer and use it in GitHub Desktop.
Save betehess/9326471 to your computer and use it in GitHub Desktop.
object~extends Application{case class T(n:String,r:Double){def+(t:T)={val p=t.r/(r+t.r);val(w,l)=if(p>0.5||(p==.5&&n<t.n))(this,t)else(t,this);w.copy(r=r+(w.r-r)/5)}
def|(o:T)=if(r>o.r||r==o.r&&n<o.n)(this,o)else(o,this)}
case class R(r:Int,a:T,b:T,w:T){override def toString=s"$r,${a.n},${b.n},${w.n}"}
def g(l:List[T]):List[(T,T)]=l match{case a::b::l⇒(a,b)::g(l);case _⇒Nil}
def p(l:List[(T,T)],r:Int,a:Seq[R]):Seq[R]=l.map{case(a,b)⇒val(c,d)=a|b;R(r,c,d,a+b)}match{case m::Nil⇒a:+m;case e⇒p(g(e.map(_.w)),r+1,a++e)}
println(p(io.Source.stdin.getLines.toList.map(_.split(",")).sortBy(_(5)).sortBy(_(4)).map{case Array(a,b,c,d,_,_)⇒(T(a,b.toDouble),T(c,d.toDouble))},1,Seq()).mkString("\n"))}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment