-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
43 lines (39 loc) · 1.03 KB
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import kotlin.math.abs
val A = 'A'.toInt()
val ZERO = '0'.toInt()
typealias Pos = Pair<Int, Int>
fun Pos.knight(other: Pos): Boolean =
when(Pair(abs(this.first - other.first), abs(this.second - other.second))) {
Pair(1, 2), Pair(2, 1) -> true
else -> false
}
fun main(args: Array<out String>) {
var first: Pos? = null
var last: Pos? = null
var before: Pos? = null
val visited = mutableSetOf<Pos>()
repeat(36) {
val current = readLine()!!.let { Pair(it[0].toInt() - A, it[1].toInt() - ZERO) }
if (current in visited) {
println("Invalid")
return
}
visited += current
last = current
if (before == null) {
first = current
before = current
return@repeat
}
if (!current.knight(before!!)) {
println("Invalid")
return
}
before = current
}
if (last!!.knight(first!!)) {
println("Valid")
} else {
println("Invalid")
}
}