-
-
Save waynejo/a3b60dd52b763a928dc9 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
package Main | |
import java.io.FileInputStream | |
import scala.collection.immutable.IndexedSeq | |
import scala.io.StdIn | |
object Main extends App { | |
Console.setIn(new FileInputStream("example.in")) | |
// Console.setIn(new FileInputStream("A-large-practice.in")) | |
// Console.setOut(new FileOutputStream("A-large-practice.out")) | |
// left=1?3, operator=+, right=24?, answer=424 | |
// 1?3 + 24? = 424 | |
// x + y = z | |
val numbers = | |
for ( | |
cIn <- 0 to 1; | |
x <- 0 to 9; | |
y <- 0 to 9; | |
z <- 0 to 9; | |
cOut <- 0 to 1) yield (cIn, x, y, z, cOut) | |
def solveSingleValue(inputCIn:String, x:String, y:String, z:String, inputCOut:String):List[(Int, Int, Int, Int, Int)] = { | |
val input = (x, y, z) | |
val results = numbers.filter { case (cIn, x, y, z, cOut) => | |
cIn + x + y == z + cOut * 10 && | |
(inputCIn == "?" || (inputCIn != "?" && inputCIn.toInt == cIn)) && | |
(input._1 == "?" || (input._1 != "?" && input._1.toInt == x)) && | |
(input._2 == "?" || (input._2 != "?" && input._2.toInt == y)) && | |
(input._3 == "?" || (input._3 != "?" && input._3.toInt == z)) && | |
(inputCOut == "?" || (inputCOut != "?" && inputCOut.toInt == cOut)) | |
} | |
results.toList | |
} | |
def getNumbers(numbers: List[((Char, Char), Char)], prev:List[(Int, Int, Int, Int, Int)] = Nil):List[(Int, Int, Int, Int, Int)] = { | |
if (numbers.isEmpty) { | |
prev | |
} else { | |
val cIn = if (1 == numbers.length) "0" else "?" | |
val (((numberX, numberY), numberZ)) = numbers(0) | |
val nextResult = if (Nil == prev) { | |
solveSingleValue(cIn.toString, numberX.toString, numberY.toString, numberZ.toString, "?") | |
} else { | |
prev.flatMap{case (cIn, x, y, z, cOut) => { | |
solveSingleValue(cIn.toString, numberX.toString, numberY.toString, numberZ.toString, cIn.toString) | |
}} | |
} | |
getNumbers(numbers.tail, nextResult) | |
} | |
} | |
def solve(left:String, operator:String, right:String, answer:String): String = { | |
println(s"left=$left, operator=$operator, right=$right, answer=$answer") | |
if ("-" == operator) { | |
val result = getNumbers(tripleZip(answer, right, left).toList) | |
println(result.mkString(", ")) | |
// val (leftResult, centerResult, rightResult) = grouping(result) | |
// println(s"$rightResult - $centerResult = $leftResult") | |
} else { | |
val result = getNumbers(tripleZip(left, right, answer).toList) | |
println(result.mkString(", ")) | |
// val (leftResult, centerResult, rightResult) = grouping(result) | |
// println(s"$leftResult + $centerResult = $rightResult") | |
} | |
"" | |
} | |
def tripleZip(x:String, y:String, z:String) = { | |
val inputs = List(x, y, z) | |
val maxLength = inputs.map(_.length).max | |
val List(newX, newY, newZ) = inputs.map(x => { | |
"0" * (maxLength - x.length) + x | |
}) | |
newX zip newY zip newZ | |
} | |
def grouping(result: IndexedSeq[(Int, Int, Int)]): (String, String, String) = { | |
val leftResult = result.map { case (x, y, z) => x }.mkString | |
val centerResult = result.map { case (x, y, z) => y }.mkString | |
val rightResult = result.map { case (x, y, z) => z }.mkString | |
(leftResult, centerResult, rightResult) | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { num => | |
val Array(left, operator, right, _, answer) = StdIn.readLine().split(" ") | |
println(s"Case #$num: ${solve(left, operator, right, answer)}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment