Skip to content

Instantly share code, notes, and snippets.

@bongster
Created February 13, 2015 13:23
Show Gist options
  • Save bongster/39f10b1bdad5917d758f to your computer and use it in GitHub Desktop.
Save bongster/39f10b1bdad5917d758f to your computer and use it in GitHub Desktop.
/**
* Created by bongster on 15. 2. 13..
*/
object Main {
def getMaxPoint(Sequence:List[List[String]] , RobotType: String, RobotX: Int, AbleTime: Int): Int =
Sequence.find(x => x(0) == RobotType) match {
case Some(idx) => idx(1).toInt > (RobotX + AbleTime) match {
case true => RobotX + AbleTime
case _ => idx(1).toInt
}
case _ => RobotX
}
def solve(Sequences : List[List[String]], RobotO: Int = 1, RobotB: Int = 1,Time: Int = 0 ): Int = {
if (Sequences.isEmpty)
Time
else{
val name = Sequences.head(0)
val point = Sequences.head(1).toInt
println(s"O : $RobotO, B: $RobotB, T: $Time, N : $name, P : $point")
name match{
case "O" => {
// RobotO == point
RobotO - point match {
case result if result < 0 => {
println("O and minus")
solve(Sequences.tail,
point,
getMaxPoint(Sequences.tail, "B", RobotB , Math.abs(result)),
Time + Math.abs(result))
}
case result if result > 0 => {
println("O and plus")
solve(Sequences.tail,point ,
getMaxPoint(Sequences.tail, "B", RobotB , Math.abs(result)),
Time + Math.abs(result) + 1)
}
case _ => {
println("O and equals")
solve(Sequences.tail, RobotO, RobotB, Time + 1)
}
}
}
case "B" => {
RobotO - point match {
case result if result < 0 => {
println("B and minus")
solve(Sequences.tail,
getMaxPoint(Sequences.tail, "O", RobotO , Math.abs(result)),
point,
Time + Math.abs(result))
}
case result if result > 0 => {
println("B and plus")
solve(Sequences.tail,
getMaxPoint(Sequences.tail, "O", RobotO , Math.abs(result)),
point,
Time + Math.abs(result) + 1)
}
case _ => {
println("B and equals")
solve(Sequences.tail, RobotO, RobotB, Time + 1)
}
}
}
}
}
}
def main(args: Array[String]) {
val test1 = "O 2 B 1 B 2 O 4".split(" ").toList.sliding(2, 2).toList
val test2 = "O 5 O 8 B 100".split(" ").toList.sliding(2, 2).toList
val test3= "B 2 B 1".split(" ").toList.sliding(2, 2).toList
println(test2)
println("===="*10)
val result = solve(test2)
println(result)
println("Hello World")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment