-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
42 lines (38 loc) · 941 Bytes
/
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
import java.util.*
class KeyLogger(val log: String) {
var cursor = 0
var buffer = LinkedList<Char>()
fun cursorLeft() {
cursor = maxOf(0, cursor - 1)
}
fun cursorRight() {
cursor = minOf(buffer.size, cursor + 1)
}
fun backspace() {
if (cursor == 0 || buffer.isEmpty()) {
return
}
buffer.removeAt(cursor - 1)
cursor -= 1
}
fun press(c: Char) {
buffer.add(cursor, c)
cursor += 1
}
fun evaluate(): String {
for (c in log) {
when (c) {
'<' -> cursorLeft()
'>' -> cursorRight()
'-' -> backspace()
else -> press(c)
}
}
return buffer.joinToString("")
}
}
fun main(args: Array<out String>) {
List(readLine()!!.toInt()) {
KeyLogger(readLine()!!).evaluate()
}.joinToString("\n").let(::println)
}