-
-
Save bongster/39f10b1bdad5917d758f 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
/** | |
* 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