์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[Programmers] Lv1. ๊ณต์›์‚ฐ์ฑ…(kotlin)

An effort will never betray ๐Ÿ˜Ž 2023. 4. 19. 01:12
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/172928

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/172928

 

 

ํ’€์ด

  • ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ธฐ๋ฒ•์œผ๋กœ ํ•˜๋‚˜์”ฉ 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)
    }
}

 

 

์ฐธ๊ณ 

 

indexOfFirst - Kotlin Programming Language

 

kotlinlang.org

 

๋ฐ˜์‘ํ˜•