์นดํ
๊ณ ๋ฆฌ ์์
[Programmers] Lv1. ๊ณต์์ฐ์ฑ (kotlin)
An effort will never betray ๐
2023. 4. 19. 01:12
๋ฐ์ํ
๋ฌธ์
์์
ํ์ด
- ์๋ฎฌ๋ ์ด์ ๊ธฐ๋ฒ์ผ๋ก ํ๋์ฉ routes์ ์กด์ฌํ๋ ๋ฐฉํฅ, ๊ฑฐ๋ฆฌ๋งํผ ์ด๋ํ ํ ํ์ธํ๋ฉด ๋ฉ๋๋ค.
- isInside ํจ์๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํด๋์ด ์ขํ ๋ฐ์ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ ์ฒดํฌ
- ์ขํ ์์ ์๋๋ผ๋ 'X' ํ๊ธฐ๋ ๋ถ๋ถ์ผ๋ก ์ฅ์ ๋ฌผ์ผ ๊ฒฝ์ฐ ์ฒดํฌ
- ์ขํ ์์ ์๊ณ ์ฅ์ ๋ฌผ๋ ์๋๋ผ๋ฉด ์์น ์ด๋
class Solution {
val dir = mapOf("E" to (0 to 1), "N" to (-1 to 0), "W" to (0 to -1), "S" to (1 to 0))
fun isInside(a: Int, b: Int, y:Int, x: Int) =
0<= a && a< y && 0<=b && b<x
fun solution(park: Array<String>, routes: Array<String>): IntArray {
var y = park.indexOfFirst{ it.contains("S")}
var x = park[y].indexOf("S")
routes.forEach{
val input = it.split(" ")
var dy = y
var dx = x
for(i in 0 until input.last().toInt()){
dy += dir[input.first()]!!.first
dx += dir[input.first()]!!.second
if(isInside(dy, dx, park.size, park[0].length).not()) break
if(park[dy][dx] == 'X') break
}
if(isInside(dy,dx, park.size, park[0].length) && park[dy][dx] != 'X'){
y = dy
x = dx
}
}
return intArrayOf(y, x)
}
}
์ฐธ๊ณ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ธ kotlin์ ํน์ฑ์ ๋ง๊ฒ ํจ์๋ฅผ ๋ง์ด ์ตํ์ ๐ฝ
- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index-of-first.html
๋ฐ์ํ