-
-
Save theodoreLee/04988ffe1a9a5470381d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream | |
import scala.io.Source | |
object CrazyRows { | |
val INPUT = "A-large-practice.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out2" | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val sets = itr.next().toInt | |
val writer = new FileOutputStream(OUTPUT) | |
// val writer = Console.out | |
try { | |
Console.withOut(writer) { | |
for (set <- 1 to sets) { | |
val matrixSize = itr.next().toInt | |
val parse = for { | |
row <- (0 until matrixSize).toList | |
} yield itr.next().lastIndexOf("1") | |
println(f"Case #${set}: ${solve(parse)}") | |
} | |
} | |
} finally { | |
writer.close() | |
} | |
} | |
def solve(list: List[Int], row: Int = 0): Int = list match { | |
case Nil => 0 | |
case x :: xs if x <= row => solve(list.tail, row + 1) | |
case _ => | |
val idx = list.indexWhere(_ <= row) | |
val (front, back) = list.splitAt(idx) | |
solve(front ++ back.tail, row + 1) + idx | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment