-
-
Save theodoreLee/656cf47ace86b0b72b80 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 Pegman { | |
val INPUT = "A-large-practice.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out2" | |
val isConsole = false | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val stream = if (isConsole) Console.out else new FileOutputStream(OUTPUT) | |
try { | |
Console.withOut(stream) { | |
val sets = itr.next().toInt | |
(1 to sets).foreach { set => | |
val Array(r, c) = itr.next().split(' ').map(_.toInt) | |
val arrays = for { | |
i <- 1 to r | |
} yield itr.next().toArray | |
println(f"Case #$set: ${arrows(r, c, arrays.toArray)}") | |
} | |
} | |
} finally { | |
stream.flush() | |
if (!isConsole) stream.close() | |
} | |
} | |
def arrows(R: Int, C: Int, array: Array[Array[Char]]): String = { | |
val arrows = (for { | |
i <- 0 until R | |
j <- 0 until C | |
} yield (i, j, array(i)(j))).filterNot(_._3 == '.') | |
def detectNextArrow(r: Int, c: Int, direction: Char): Boolean = direction match { | |
case '^' => (0 until r).map(i => array(i)(c)).exists(_ != '.') | |
case 'v' => (r + 1 until R).map(i => array(i)(c)).exists(_ != '.') | |
case '<' => (0 until c).map(j => array(r)(j)).exists(_ != '.') | |
case '>' => (c + 1 until C).map(j => array(r)(j)).exists(_ != '.') | |
} | |
val mismatchArrows = arrows.filterNot { | |
case (r, c, value) => detectNextArrow(r, c, value) | |
} | |
val result = mismatchArrows.map { | |
case (r, c, value) => | |
arrows.find { | |
case (_r, _c, _value) if r == _r && c != _c => true | |
case (_r, _c, _value) if c == _c && r != _r => true | |
case (_r, _c, _value) => false | |
} | |
} | |
if (result.contains(None)) { | |
"IMPOSSIBLE" | |
} else { | |
result.length.toString | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment