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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| class Solution {
fun solution(park: Array<String>, routes: Array<String>): IntArray {
val parkHash = HashMap<String, Char>()
var currentPoint = ""
var row = 0
var column = 0
// parkHash 값 채워넣기
// ex) key: 2.1 value: S (O or X)
park.forEach { _row ->
_row.toList().forEach { status ->
val key = "$column.$row"
parkHash[key] = status
if (status == 'S') currentPoint = key
row++
}
row = 0
column++
}
// routes 계산
routes.forEach { route ->
val routeSplited = route.split(" ")
val status = routeSplited.first()
val value = routeSplited.get(1).toInt()
when (status) {
"E" -> {
currentPoint.getRoutedKey(0, value)
}
"W" -> {
currentPoint.getRoutedKey(0, -value)
}
"S" -> {
currentPoint.getRoutedKey(value, 0)
}
"N" -> {
currentPoint.getRoutedKey(-value, 0)
}
else -> null
}?.let { nextPoints ->
// FIXME 결과말고 과정에서 막히는지 체크 필요
var isObstacle = false
nextPoints.forEach { nextPoint ->
if (parkHash.get(nextPoint) != 'O') {
isObstacle = true
return@forEach
}
}
if (!isObstacle && nextPoints.isNotEmpty()) {
parkHash.set(currentPoint, 'O')
currentPoint = nextPoints.last()
}
}
}
return currentPoint.split(".").map { it.toInt() }.toIntArray()
}
private fun String.getRoutedKey(column: Int = 0, row: Int = 0): List<String> {
val seperator = "."
val seperatedValue = this.split(seperator)
val strList = arrayListOf<String>()
var _column = seperatedValue.get(0).toInt()
var _row = seperatedValue.get(1).toInt()
if (column != 0) {
for (i in 1.. Math.abs(column)) {
if (column > 0) _column += 1 else _column -= 1
strList.add("$_column$seperator$_row")
}
} else {
for (i in 1.. Math.abs(row)) {
if (row > 0) _row += 1 else _row -= 1
strList.add("$_column$seperator$_row")
}
}
return strList.toList()
}
}
|