https://school.programmers.co.kr/learn/courses/30/lessons/172928
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
class Solution {
fun solution(park: Array<String>, routes: Array<String>): IntArray {
var answer: IntArray = intArrayOf(0,0)
var command = HashMap<String, IntArray>()
command.put("E", intArrayOf(0,1))
command.put("W", intArrayOf(0,-1))
command.put("S", intArrayOf(1,0))
command.put("N", intArrayOf(-1,0))
for (i in 0 .. park.size-1) {
for (j in 0 .. park[i].length-1) {
if (park[i][j] == 'S') {
answer = intArrayOf(i,j)
}
}
}
routes.forEach {
var direction = command[it.split(" ")[0]]
var count = it.split(" ")[1].toInt()
var row = answer[0]
var column = answer[1]
for (i in 1..count) {
row += direction!![0]
column += direction!![1]
if(row < 0 || column < 0 || row >= park.size || column >= park[0].length || park[row][column] == 'X') {
row = answer[0]
column = answer[1]
break
}
}
answer[0] = row
answer[1] = column
}
return answer
}
}
fun solution(park: Array<String>, routes: Array<String>): IntArray {
var currY = 0
var currX = 0
val moveY = mapOf('N' to -1, 'S' to 1, 'W' to 0, 'E' to 0)
val moveX = mapOf('N' to 0, 'S' to 0, 'W' to -1, 'E' to 1)
for (i in park.indices) {
for (j in park[i].indices) {
if (park[i][j] == 'S') {
currY = i
currX = j
}
}
}
for (route in routes) {
val direction = route[0]
val step = route.substring(2).toInt()
var maxStep = step
for (i in 1..step) {
val newY = currY + moveY[direction]!! * i
val newX = currX + moveX[direction]!! * i
if (newY < 0 || newX < 0 ||newY >= park.size || newX >= park[0].length || park[newY][newX] == 'X') {
maxStep = 0
break
}
}
currY += moveY[direction]!! * maxStep
currX += moveX[direction]!! * maxStep
}
return intArrayOf(currY, currX)
}
'Code KATA' 카테고리의 다른 글
Programmers 최빈값 구하기 with Kotlin (0) | 2024.01.11 |
---|---|
Programmers 달리기 경주 with Kotlin (0) | 2023.12.28 |
Programmers 개인정보 수집 유효기간 with Kotlin (0) | 2023.12.27 |
Programmers 바탕화면 정리 with Kotlin (0) | 2023.12.22 |
Programmers 성격 유형 검사하기(2022KaKaoTech) with Kotlin (1) | 2023.12.21 |