문제링크
fun Int.toIntList(): List<Int> {
val intList = mutableListOf<Int>()
var temp = this
while (temp != 0) {
intList.add(temp % 10)
temp /= 10
}
return intList
}
fun main() {
val bufferedReader = System.`in`.bufferedReader()
val bufferedWriter = System.out.bufferedWriter()
var candidateNumbers = createCandidateNumbers()
val n = bufferedReader.readLine().toInt()
repeat(n) {
val (number, strike, ball) = bufferedReader.readLine().split(" ").map { it.toInt() }
candidateNumbers = candidateNumbers.filter { candidateNumber ->
val resultStrike = getStrike(candidateNumber, number.toIntList())
val resultBall = getBall(candidateNumber, number.toIntList())
isSameStrikeAndBall(resultStrike, resultBall, strike, ball)
}.toMutableList()
}
bufferedWriter.write("${candidateNumbers.size}")
bufferedReader.close()
bufferedWriter.close()
}
fun createCandidateNumbers(): MutableList<List<Int>> {
val candidateNumbers = mutableListOf<List<Int>>()
for (i in 1..9) {
for (j in 1..9) {
for (k in 1..9) {
if (areDistinctNumbers(i, j, k)) {
candidateNumbers.add(listOf(i, j, k))
}
}
}
}
return candidateNumbers
}
fun areDistinctNumbers(firstNumber: Int, secondNumber: Int, thirdNumber: Int): Boolean {
return firstNumber.xor(secondNumber) * secondNumber.xor(thirdNumber) * thirdNumber.xor(firstNumber) != 0
}
fun getStrike(candidateNumber: List<Int>, inputNumber: List<Int>): Int {
var strike = 0
for (i in candidateNumber.indices) {
if (candidateNumber[i] == inputNumber[i]) strike++
}
return strike
}
fun getBall(candidateNumber: List<Int>, inputNumber: List<Int>): Int {
var ball = 0
for (i in candidateNumber.indices) {
for (j in inputNumber.indices) {
if (i != j && candidateNumber[i] == inputNumber[j]) ball++
}
}
return ball
}
fun isSameStrikeAndBall(resultStrike: Int, resultBall: Int, inputStrike: Int, inputBall: Int): Boolean {
return resultStrike == inputStrike && resultBall == inputBall
}
주석 없는 코드를 만들기 위해 노력하는 개발자입니다.
혹시라도 의도가 분명하지 않아보이는 (이해가 되지 않는) 코드가 있으시다면 편하게 답변 달아주시면 정말 감사하겠습니다.