보물지도 그리기 봇 (참고 : 답에 1

친구를 위해 보물 찾기를 조직하고 있습니다. 더 쉽게 일을 수행하기 위해 귀중한 물건을 숨긴 모든 위치의지도를 그리려고합니다.

입력

0 0왼쪽 상단에있는 (음수가 아닌) x 및 y 좌표로 구성된 점 목록을 나타내는 모든 형식의 입력 이 허용됩니다 (참고 : 답에 1 기반 색인을 사용할 수도 있습니다. ). 예:

1 2
3 0
0 1

도전

함수 또는 프로그램은 x출력에서 y + 1 행과 x + 1 열에 표시가있는 모든 주어진 위치를 나타내는 맵을 구성 할 수 있어야합니다 . 표시되지 않은 위치는로 표시됩니다 . 맵은 또한 코너는 프레임 구성 +들, 수직선은 |S는 상기 수평 라인은 -S. 솔루션은 가능한 가장 작은 프레임을 출력해야합니다. 위에 주어진 입력 예제에 대한 맵 :

+----+
|   x|
|x   |
| x  |
+----+

가능한 테스트 사례


"0 0"
=>
+-+
|x|
+-+

"0 10
 5 5
 10 0"
=>
+-----------+
|          x|
|           |
|           |
|           |
|           |
|     x     |
|           |
|           |
|           |
|           |
|x          |
+-----------+

""
=>
++
++

"0 0
 0 2
 2 0"
=>
+---+
|x x|
|   |
|x  |
+---+

물론 이것은 이므로 바이트 수가 가장 적은 솔루션이 승리합니다! 솔루션 설명이 권장됩니다.



답변

J , 37 34 바이트

0<@|:' x'{~((i.@]e.#.~)1+>./) ::#:

온라인으로 사용해보십시오!

                       1+>./          maximum for each coordinate + 1
             i.@]                     make an array with these dimensions filled with 0..x*y
                                      /* if the input is empty,
                                         1+>./ is negative infinity
                                         and i.@] throws an error  */
                   #.~                mixed base conversion of input
                 e.                   replace the elements of i.@]
                                        with 1 if it's present in the
                                        converted input, 0 otherwise
           (                ) ::      if there's an error do the other thing instead
                                #:    "to binary", for empty input this returns a 0x0 matrix
0<@|:' x'{~                           index into character string, transpose and put in a box

답변

자바 스크립트 (ES6), 150 바이트

입력을 1- 인덱스 좌표 목록으로 [x,y]형식화합니다. 문자열을 반환합니다.

a=>(g=w=>y<h?' |-+x'[4*a.some(a=>a+''==[x,y])|2*(-~y%h<2)|++x%w<2]+[`
`[x=x<w?x:+!++y]]+g(w):'')((M=i=>Math.max(2,...a.map(a=>a[i]+2)))(x=y=0),h=M(1))

온라인으로 사용해보십시오!


답변

하스켈 , 127123 바이트

이것은 x 좌표 목록과 해당 y 좌표 (!)목록을 취하는 연산자 를 정의합니다 .

x!y|l<-'+':('-'<$m x)++"+"=unlines$l:['|':[last$' ':['x'|(i,j)`elem`zip x y]|i<-m x]++"|"|j<-m y]++[l];m x=[1..maximum$0:x]

온라인으로 사용해보십시오!

언 골프 / 설명

도우미 함수 m는 목록을 예상하고 목록을 비우면 최대 (1부터 시작)까지 인덱스를 반환합니다 [].

m x | null x    = []
    | otherwise = [1 .. maximum x]

실제 연산자 (!)는 모든 좌표를 순회 하고 개행 문자와 결합되는 문자 또는 x문자를 선택하는 목록 이해입니다 .

x ! y
  -- construct the top and bottom line
  | l <- "+" ++ replicate (maximum (0:x)) '-' ++ "+"
  -- join the list-comprehension with new-lines
  = unlines $
  -- prepend the top line
      [l]
  -- the actual map:
    -- begin the line with | and add the correct chars for each coordinate
      ++ [ "|" ++ [ if (i,j) `elem` zip x y then 'x' else ' '
    -- "loop" over all x-coordinates
                 | i <- m x
                 ]
    -- end the line with a |
           ++ "|"
    -- "loop" over all y-coordinates
         | j <- m y
         ]
  -- append the bottom line
      ++ [l]

답변

캔버스 , 22 바이트

ø╶{X;┤╋}l|*eL┤-×+e:└∔∔

여기 사용해보십시오!

1 인덱스 입력을받습니다.

마침내 나에게 성가신 버그를 고치기로 결정하고 이것을 21 바이트 로 줄였습니다. .

설명 (단일 공간의 경우 절반 -ASCII-fied) :

ø╶{X;┤╋}l|*eL┤-×+e:└++  full program, implicitly outputting ToS at the end
ø                       push an empty Canvas - the map
 ╶{    }                for each array in the input array
   X                      push "X"
    ;┤                    and push the two coordinates separately on the stack
      ╋                   and overlap the "X" there in the map
        l               get the vertical length of the map
         |*             repeat "|" vertically that many times
           e            encase the map in two of those vertical bars
            L           get the horizontal length of the map
             ┤          subtract 2 (leave place for the "+"es)
              -×        repeat "-" that many times
                +e      encase that line in "+"es
                  :└    push a copy of that below the map
                    ++  and join the 3 items vertically

답변

파이썬 (2) , 151 (140) 138 바이트

Jo King 덕분에 -2 바이트

입력은 1 인덱스입니다.

m=input()
w,h=map(max,zip((0,0),*m))
b=['+'+'-'*w+'+']
M=b+['|'+' '*w+'|']*h+b
for x,y in m:M[y]=M[y][:x]+'x'+M[y][x+1:]
print'\n'.join(M)

온라인으로 사용해보십시오!


답변

, 37 바이트

≔E²⁺²⌈Eθ§λιηB⊟⮌η⊟ηFθ«J⊟⮌ι⊟ιx

온라인으로 사용해보십시오! 링크는 자세한 버전의 코드입니다. 1- 색인. 설명:

¿¬LθUR²+«

2×2 직사각형의 +s를 그려서 특수한 경우 빈 입력 .

≔E²⁺²⌈Eθ§λιη

입력을 바꾸고 각 열 (현재 행)의 최대 값을 취하고 2를 더하여 상자 크기를 숯 좌표로 가져옵니다.

B⊟⮌η⊟η

상자를 그립니다.

Fθ«

각 좌표를 반복합니다.

J⊟⮌ι⊟ι

해당 위치로 이동하십시오.

x

십자가로 표시하십시오.


답변

Stax , 32 31 24 바이트

╩╠ee%╙æM■↓^⌐╧ΩΓ¡c¥èf¢○ [

실행 및 디버깅

0부터 시작하는 인덱스를 [y, x]쌍의 배열로 취합니다 .

설명:

zs'X&|<cM%'-*'+|S]s{'||Smn++m Unpacked program, implicit input
zs                            Tuck empty array under input
  'X                          Push "X"
    &                         Assign element at all indices (create map)
                                As the indexing arrays are an array of arrays, treat them as a path to navigate a multidimensional array.
                                Extend array if needed.
     |<                       Left-align all to the length of the longest.
       cM%                    Copy, transpose, length (width)
          '-*                 Repeat "-"
             '+|S             Surround with "+"
                 ]s           Make a singleton and tuck it below the map
                   {    m     Map:
                    '||S        Surround with "|"
                         n++  Surround with the above/below border (built above)
                            m Map:
                                Implicit output