Skip to content

Instantly share code, notes, and snippets.

@sheki
Created April 9, 2011 17:17
Show Gist options
  • Save sheki/911567 to your computer and use it in GitHub Desktop.
Save sheki/911567 to your computer and use it in GitHub Desktop.
Scala code to solve. http://goo.gl/B2tS9 Google code jam
Arrays received
1
3
-5
-2
4
1
scala.MatchError: ArraySeq(-5, 1, 3)
at Main$$anon$1.scalarProduct$1(minscalarProduct.scala:9)
at Main$$anon$1.Main$$anon$$minScalarProduct(minscalarProduct.scala:14)
at Main$$anon$1$$anonfun$processInput$1.apply(minscalarProduct.scala:22)
at Main$$anon$1$$anonfun$processInput$1.apply(minscalarProduct.scala:21)
at scala.collection.Iterator$class.foreach(Iterator.scala:631)
at scala.collection.Iterator$$anon$27.foreach(Iterator.scala:578)
at Main$$anon$1.processInput(minscalarProduct.scala:21)
at Main$$anon$1.<init>(minscalarProduct.scala:27)
at Main$.main(minscalarProduct.scala:1)
at Main.main(minscalarProduct.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:81)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:86)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:81)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:86)
at scala.tools.nsc.ScriptRunner$.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:241)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:265)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:265)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$2.apply(ScriptRunner.scala:225)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$2.apply(ScriptRunner.scala:225)
at scala.Option.map(Option.scala:129)
at scala.tools.nsc.ScriptRunner$.withCompiledScript(ScriptRunner.scala:225)
at scala.tools.nsc.ScriptRunner$.runScript(ScriptRunner.scala:265)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:91)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
2
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1
def minScalarProduct( x : Seq[Int],y : Seq[Int] ) : Int = {
println("Arrays received")
x foreach (println(_))
y foreach (println(_))
val sortedX = x.sorted
val sortedY= y.sortWith(_>_)
def scalarProduct(x: Seq[Int], y: Seq[Int] ) : Int = x match {
case Seq() => 0
case x1::xs1 => x1 * y.head + scalarProduct ( xs1, y.tail )
}
scalarProduct(sortedX,sortedY)
}
def processInput(source: io.Source) {
val groups = source.getLines.drop(1).grouped(3)
groups.zipWithIndex foreach { e =>
val solution = minScalarProduct ( e._1(1).split(" ").map(_.toInt), e._1(2).split(" ").map(_.toInt))
println("Case #"+(e._2+1)+": "+solution)
}
}
processInput(io.Source.fromFile("input.txt"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment