ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그래머스 / 당구 연습
    코딩 테스트/Level 1 2023. 3. 17. 11:27
    반응형

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

    경계면을 기준으로 '접힌 상태를 편다'라고 생각하면 간단하다..
    '피타고라스의 정의'를 이용해 거리를 계산.   

    파이썬 

    def solution(m, n, sx, sy, balls):
        return [
            min((sx - x) ** 2 + (sy - y) ** 2
                for x, y in ((-bx, by), (bx, -by), (2 * m - bx, by), (bx, 2 * n - by))
                if not ((sx == bx and (sy > by > y or sy < by < y))
                        or
                        (sy == by and (sx > bx > x or sx < bx < x)))
                )
            for bx, by in balls
        ]

    이중 for 문을 이용한 리스트 컴프리헨션은 가독성이 좋진 않지만, 재미있는 코테를 위하여.. 

     

    시작...

    def solution(m, n, start_x, start_y, balls):
        answer = []
        for ball_x, ball_y in balls:
            temp = []
            for x, y in ((-ball_x, ball_y), (ball_x, -ball_y), (2 * m - ball_x, ball_y), (ball_x, 2 * n - ball_y)):
                if start_x == ball_x:
                    if start_y > y:
                        if start_y > ball_y > y:
                            continue
                    elif start_y < y:
                        if start_y < ball_y < y:
                            continue
                elif start_y == ball_y:
                    if start_x > x:
                        if start_x > ball_x > x:
                            continue
                    elif start_x < x:
                        if start_x < ball_x < x:
                            continue
                temp.append((start_x - x) ** 2 + (start_y - y) ** 2)
            answer.append(min(temp))
        return answer

    줄여보자. 

    def solution(m, n, start_x, start_y, balls):
        answer = []
        for ball_x, ball_y in balls:
            temp = []
            for x, y in ((-ball_x, ball_y), (ball_x, -ball_y), (2 * m - ball_x, ball_y), (ball_x, 2 * n - ball_y)):
                if start_x == ball_x:
                    if start_y > ball_y > y or start_y < ball_y < y:
                        continue
                elif start_y == ball_y:
                    if start_x > ball_x > x or start_x < ball_x < x:
                        continue
                temp.append((start_x - x) ** 2 + (start_y - y) ** 2)
            answer.append(min(temp))
        return answer

    줄여보자. 

    def solution(m, n, start_x, start_y, balls):
        answer = []
        for ball_x, ball_y in balls:
            temp = []
            for x, y in ((-ball_x, ball_y), (ball_x, -ball_y), (2 * m - ball_x, ball_y), (ball_x, 2 * n - ball_y)):
                if start_x == ball_x and (start_y > ball_y > y or start_y < ball_y < y):
                    continue
                elif start_y == ball_y and (start_x > ball_x > x or start_x < ball_x < x):
                    continue
                temp.append((start_x - x) ** 2 + (start_y - y) ** 2)
            answer.append(min(temp))
        return answer

    줄여보자.. 

    def solution(m, n, start_x, start_y, balls):
        answer = []
        for ball_x, ball_y in balls:
            temp = []
            for x, y in ((-ball_x, ball_y), (ball_x, -ball_y), (2 * m - ball_x, ball_y), (ball_x, 2 * n - ball_y)):
                if (start_x == ball_x and (start_y > ball_y > y or start_y < ball_y < y)) or \
                        (start_y == ball_y and (start_x > ball_x > x or start_x < ball_x < x)):
                    continue
                temp.append((start_x - x) ** 2 + (start_y - y) ** 2)
            answer.append(min(temp))
        return answer

    리스트 컴프리헨션으로 마무리하자. 

     

    코틀린

    import kotlin.math.*
    
    class Solution {
        fun solution(m: Int, n: Int, sX: Int, sY: Int, balls: Array<IntArray>): IntArray {
            return balls.map { (bX, bY) ->
                arrayOf(-bX to bY, bX to -bY, 2 * m - bX to bY, bX to 2 * n - bY)
                    .filter { (x, y) ->
                        !((sX == bX && (bY in y + 1 until sY || bY in sY + 1 until y))
                                || (sY == bY && (bX in (x + 1) until sX || bX in sX + 1 until x)))
                    }.minOf { (x, y) ->
                        (sX - x.toDouble()).pow(2) + (sY - y.toDouble()).pow(2)
                    }.toInt()
            }.toIntArray()
        }
    }
    반응형
Designed by Tistory.