Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active August 29, 2015 14:15
Show Gist options
  • Save sungkmi/69b01c67378060d19511 to your computer and use it in GitHub Desktop.
Save sungkmi/69b01c67378060d19511 to your computer and use it in GitHub Desktop.
object BotTrust extends App {
case class Simulation(time: Int, orange: Int, blue: Int) {
def +(order: (String, Int)): Simulation = order match {
case ("O", button) =>
Simulation(time + (button - orange).abs + 1, button, blue)
case ("B", button) =>
Simulation(time + (button - blue).abs + 1, orange, button)
}
def result: Int = {
time
}
}
// case class Bot(location: Int) {
// // private var currentJob:Option[Int] = manager.ge
// private var position: Int = 1
//
// def work = {
//
// }
// }
def minTime(order: List[(String, Int)], simulation: Simulation = Simulation(0, 1, 1)): Int = {
println(simulation)
order match {
case Nil =>
simulation.result
case (bot, button) :: tail =>
minTime(tail, simulation + (bot, button))
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val order = ("4 O 2 B 1 B 2 O 4" split ' ').tail.grouped(2).map { case Array(bot, button) => (bot, button.toInt) }.toList
lineOut(s"Case #$i: ${minTime(order)}")
}
val writer = new java.io.PrintWriter("b.large.out")
try {
process(io.Source.fromFile("B-large-practice.in").getLines) { s =>
writer.println(s)
writer.flush()
}
} finally {
writer.flush(); writer.close()
}
}
import org.scalatest._
import BotTrust._
class BotTrustTest extends FunSuite {
test("base #1") {
assert(minTime(List()) === 0 )
}
test("base #2") {
assert(minTime(List(("O",1))) === 1 )
}
test("base #3") {
assert(minTime(List(("O",2))) === 2 )
}
test("base #4") {
assert(minTime(List(("O",2), ("B", 1))) === 3 )
}
ignore("base #5") {
assert(minTime(List(("O",2), ("B", 2))) === 3 )
}
ignore("sample #1") {
assert(minTime(List(("O",2), ("B",1), ("B",2), ("O",4))) === 6 )
}
ignore("sample case") {
val input = """3
115
1051
6233""".lines
val expected = """Case #1: 151
Case #2: 1105
Case #3: 6323""".lines
lineComparison(input, expected)
}
ignore("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("b.small.out.ref").getLines()
lineComparison(input, expected)
}
ignore("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("b.large.out.ref").getLines()
lineComparison(input, expected)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
process(input) { s =>
for (line <- s.lines) assert(line.trim === expected.next().trim)
}
assert(expected.hasNext === false, "Finished too fast.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment