-
Turns on RoadmyCode/ShortestCodeChallenge 2016. 5. 17. 20:102000

There's a wolf who lives in the plane forest, which is located on the Cartesian coordinate system. When going on the hunt, the wolf starts at point
(0, 0)and goes spirally as shown in the picture below:
Example
- For
x = 1andy = 1, the output should beturnsOnRoad(x, y) = 1. - For
x = 1andy = -1, the output should beturnsOnRoad(x, y) = 4.
Input/Output
[input] integer x
xcoordinate of the final point.-20 ≤ x ≤ 20.[input] integer y
ycoordinate of the final point.-20 ≤ y ≤ 20.[output] integer
The number of turns.
From - https://codefights.com/challenge/7WzF2fGJA6nKaMbNR
상단의 그래프에서 보이는 특정한 패턴을 가지고 이동하는 늑대가 특정한 좌표 (x,y)에 있을때
그때까지 총 몇번을 돌았을까 찾는 문제다.
경우의 수를 찾아가면서, 특정 좌표에 따라 어떤 일정화된 패턴 식에 대입을 시키면 아주 간단한 문제가 아닐까 싶었다.
최대한 논리적으로 깔끔하고 짧게 짜려면, 겹치는 경우의 패턴을 얼마나 잘 잡아내느냐가 관건이라고 판단했다.
그리고 내 코드.
x, y의 절대값의 좌표와 그 크기의 비교를 이용해서 적절히 경우의 수를 나눈다면
점화식 [4k-3] 에다가 특정 값을 증감시키면 답이 나오는 것을 발견해서 그대로 식으로 옮겼다.
글자 수는 총 209자.
이번에 5등이나 햇따 우왕아아아아!!
1등은 89자로 했던데 어떻게한거지...
'myCode > ShortestCodeChallenge' 카테고리의 다른 글
Reverse_t9 (0) 2016.06.08 Cipher_Zeroes [Decimal to Binary] (0) 2016.06.04 Anagram (0) 2016.05.14 Bridge (0) 2016.05.11 FindBox (0) 2016.04.28 - For