トレジャーマップ描画ボット


14

あなたはあなたの友人のために宝探しを組織しています。物事をより簡単に行うには、貴重なオブジェクトを隠したすべての場所の地図を描きたいと思います。

入力

(負ではない)x座標とy座標で構成0 0され、左上隅である点のリストを示す任意の形式の入力が許可されます(注:回答で1ベースのインデックスを使用することもできます。 )。例:

1 2
3 0
0 1

チャレンジ

関数またはプログラムはx、出力内の行y + 1および列x + 1にマークがある場所で、指定されたすべての場所を示すマップを構築できる必要があります。マークされていない場所はで表されます。マップは、角が+s、垂直線が|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  |
+---+

もちろん、これは。つまり、バイト数が最も少ないソリューションが勝ちます。ソリューションの説明をお勧めします。


本当ではありませんが、別の入力形式については本当に考えられませんでした。しかし、もしそれが課題に利益をもたらすならば、私はそれを喜んで変えます。
racer290

非正方形の地図はありますか?
FrownyFrog

4
@ racer290次のようなことを言うことをお勧めしますthe input is a list of locations (e.g. nested list, list of tuples, space & newline separated, separate inputs, ect.)
dzaima

1
出力は文字の2D配列にできますか?
-ovs

2
x座標とy座標を2つの別個の引数として受け取る関数を送信できますか?
ბიმო

回答:


7

J37 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

1
出力フォーマットは、私が提案したフォーマットよりも優れていると思います;)
racer290

なぜ::emptyそんなに冗長なのですか?どうしたの?なぜ1バイトほどに単純化できないのですか?(私はJの知識がありません)
魔法のタコ

私は:: emptyなしでTIOでそれを実行し、それはうまくいくよう
-Quintec

実は::空「は、」入力の場合扱えるように思われる
Quintec

@MagicOctopusUrn真に空のボックスを出力するより短い方法は知りません。デフォルトでは1行です。
FrownyFrog

4

JavaScript(ES6)、150バイト

入力を[x,y]形式の1インデックス付き座標のリストとして受け取ります。文字列を返します。

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))

オンラインでお試しください!


4

Haskell127 123バイト

これは、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]

実際の演算子(!)は単なるリスト理解であり、すべての座標をトラバースし、or 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]

3

キャンバス、22 バイト

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

ここで試してみてください!

1インデックス付きの入力を受け取ります。

最終的に私は長い間私を悩ませていたバグを修正することにし、これを21バイトにまで減らしました

説明(固定幅の半角ASCIIフィード):

ø╶{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

3

パイソン2151の 140 138バイト

ジョーキングのおかげで-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)

オンラインでお試しください!


1ベースのインデックス作成を使用しているのではないかと思われます。チャレンジに記載されているように、回答にメモを残してください。
racer290

2

、37バイト

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

オンラインでお試しください!リンクは、コードの詳細バージョンです。1インデックス付き。説明:

¿¬LθUR²+«

+sの2x2の長方形を描くことによる、特別な場合の空の入力。

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

入力を転置し、各列(現在の行)の最大値を取得し、2を追加して、木炭座標のボックスサイズを取得します。

B⊟⮌η⊟η

ボックスを描きます。

Fθ«

各座標をループします。

J⊟⮌ι⊟ι

その位置にジャンプします。

x

十字マークを付けます。


空の入力のために失敗するようだ:tio.run/...
wastl

@wastlありがとう、回避策を思いついた。
ニール

2

Stax32 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

1
よくできました。|Sサラウンドインストラクションと、後続の速記マップからもう少しマイレージを得ることができます。(m)サラウンド取りa及びbスタックから及び生成しますb+a+b。またm、finalの代わりにを使用|Jして行を反復処理し、出力を生成できます。 たとえば、
再帰的な

1
もう一つ:あなたは置き換えることができz]n+H%cM%。これは、マップの幅を取得する部分ですが、空のマップには特別なケースがあります。マップを測定する前に転置すると、特別なケースはなくなります。
再帰的

@recursive私はサラウンドのようなものを探していたが、私は間違っている、キーワード検索
wastl

その操作を自然に何と呼びますか?次の人が見つけられるように、ドキュメントに追加するかもしれません。
再帰的

@recursiveそれが何であったか覚えていない、そして今私は自然にそれを今サラウンドと呼ぶだろう
wastl

2

R133125122バイト

function(m)cat(z<-c("+",rep("-",u<-max(m[,1])),"+","
"),rbind("|",`[<-`(matrix(" ",u,max(m[,2])),m,"x"),"|","
"),z,sep="")

オンラインでお試しください!

1インデックス付き。引数として行列を取ります。digEmAllのおかげで8バイト、Giuseppeのおかげで3バイト節約されました!説明(以前のバージョンのコード):

function(m){                           #x and y are the 1st and 2nd col of m
s=matrix(32,u<-max(m[,1]),max(m[,2]))  #s (treasure map) has dim max(x), max(y) 
s[m]=120                               #place the X's on the map
cat(                                   #print:
    z<-c("+",rep("-",u),"+","\n"),     #the top line
    intToUtf8(rbind(124,s,124,13)),    #the map
    z,                                 #the bottom line.
    sep="")
}

utf8コードの代わりに通常の文字を使用する場合、8文字を保存します:tio.run / ##ZU7NDoIwDL7zFEu9tKEzDONF4UkMhzmGchgYNhKC@uwIaozRpG36 /
##ZU7NDoIwDL7zFEu9tKEzDONF4UkMhzmGchgYNhKC@uwIaozRpG36

使用して122バイト[<-中括弧を削除するために直接して。
ジュゼッペ

@Giuseppe確かに!方法がなければならないことは知っていました。
JayCe

1

[y、x]形式の座標

JavaScript(Node.js)191 184バイト

c=f=a=>{a.map(([y,x])=>(c[M<++y?M=y:y]=c[y]||[])[m<++x?m=x:x]="x",M=m=0)
m++
M++
s=""
for(i=0;i<=M;s+=`
`,i++)for(j=0;j<=m;j++)s+=(c[i]||0)[j]||(j%m?i%M?" ":"-":i%M?"|":"+") 
return s}

オンラインでお試しください!


誤ってx座標とy座標をどこかで交換したと思います
。– racer290

@ racer290をもっと具体的に教えてください。
ダニエルインディー

ソリューションを試してみたところ、テストケースのx座標を変更すると、座標の垂直方向が変化することがわかりました。バグは最初の行(a.map(([y,x])
racer290

しかし、xはテストケースで見られるように、右PARAMTERある
DanielIndie

2
あなたのソリューションでは、最初にy座標を取得しますか?そのときは、答えにメモを残した方が良いと思います。
racer290

1

JavaScript、180バイト

F = 

s=>s.map(([x,y])=>(t[y]=t[Y<y?Y=y:y]||[])[X<x?X=x:x]='x',t=[X=Y=0])&&[...t,0].map((_,y)=>[...Array(X+2)].map((_,x)=>[(t[y]||0)[x]||' ',...'-|+'][!(y%~Y)+2*!(x%~X)]).join``).join`
`


console.log(F([[1,11],[6,6],[11,1]]))


1

Java 10、238 223バイト

c->{var r="";int w=0,h=0,x,y;for(var l:c){w=(x=l.get(0))>w?x:w;h=(y=l.get(1))>h?y:h;}for(w++,h++,x=-1;++x<=w;r+="\n")for(y=-1;++y<=h;)r+=x%w+y%h<1?"+":x%w<1?"-":y%h<1?"|":(c+"").contains("["+x+", "+y+"]")?"x":" ";return r;}

1インデックス付きの座標。

オンラインでお試しください。

説明:

c->{                      // Method with 2D Lists as parameter and String return-type
  var r="";               //  Result-String, starting empty
  int w=0,h=0,            //  Width and height, starting at 0
      x,y;                //  Temp x,y coordinates
  for(var l:c){           //  Loop over the Inner Lists containing the coordinates
    w=(x=l.get(0))>w?x:w; //   Determine width based on max x-coordinate
    h=(y=l.get(1))>h?y:h;}//   Determine height based on max y-coordinate
  for(w++,h++,            //  Increase both the width and height by 1
      x=-1;++x<=w;        //  Loop `x` in the range [0, width]
      r+="\n")            //    After every iteration: append a new-line to the result
    for(y=-1;++y<=h;)     //   Inner loop `y` in the range [0, height]
      r+=                 //    Append the following character to the result-String:
        x%w+y%h<1?        //    If it's one of the corners:
          "+"             //     Append "+"
        :x%w<1?           //    Else-if it's the top or bottom row:
          "-"             //     Append "-"
        :y%h<1?           //    Else-if it's the right or left column:
          "|"             //     Append "|"
        :(c+"").contains("["+x+", "+y+"]")? 
                          //    Else-if the current `x,y` is part of the input-coordinates
          "x"             //     Append "x"
        :                 //    Else:
          " ";            //     Append " "
  return r;}              //  Return the result-String

rwhxy; lcwxlgetw?xw; ヒュルゲス?yh; forwhxxwr。foryyhrxwyh?xwyhcxy?xr。
魔法のタコ

@MagicOctopusUrnすべての変数とget/の名前は何forですか?:S XD
ケビンCruijssen

1

C(gcc)246 234バイト

提案してくれたceilingcatに感謝します。

ゼロインデックス。この関数は、座標とバッファーのリストを受け取り、最大のx値とy値を見つけ、バッファーにスペースを入れ、フレームを生成し、「x」をプロットします。

f(int*a,char*c){int*b=a,x,y=x=-1,i=0;for(;~*b;*++b>y?y=*b:0,++b)*b>x?x=*b:0;for(x+=4,y+=3,memset(c,32,x*y);++i<x;c[i]=c[y*x-i]=45);for(i=0;i<y;c[x*++i-1]=10*(i<=y))c[x*i]=c[x*i+x-2]=i&&y/i?124:43;for(b=a;~*b;b+=2)c[*b+1-~b[1]*x]='x';}

オンラインでお試しください!



1

05AB1E44 42バイト

ζεZ}>`UX'-×'+.ø©,F'|NVXF¹YN‚.å„ xè}'|J,}®,

オンラインでお試しください!


 ζεZ}>`                                     # Push the max of X and Y to the stack +1.
       UX                                   # Store the max X.
         '-×'+.ø©,                          # Print the top border.
                  F                     }   # From 0 to Y...
                   '|                       # Push left border.
                     NV                     # Store current Y in Y.
                       XF          }        # From 0 to X...
                         ¹                  # Push input.
                          YN‚               # Group current X and Y.
                             .å             # Exists in original input ? 1 : 0
                               „ xè         # Exists ? 'X' : ' '
                                    '|J,    # Right border, join, print.
                                         ®, # Print bottom border.

XとYは逆になる可能性があり、それがまったく問題になるかどうかは知りませんでした。


私はこれをより少ないバイトで持っていると思いますが、我々は見るでしょう... いや。

ζεZ}>`D'-×'+.øUð×'|.øs.D)X.ø©svy>`s®sUXès'xsǝXǝ}

1
それほど多くはありませんが、最初のFLv、削除NV、に変更Yすることで1バイト節約できますy41バイト
ケビンクルーッセン

1
チャット@Emigna述べたように、εZ}ができます€à
ケビンクルーイッセン

モバイルでこれを編集するのが嫌いで、PCに近づくまで待機します。
魔法のタコ

1
@KevinCruijssenでÝvはありませんLvが、それでも良い編集です:)。
魔法のタコ

ああ、あなたは正しい。Ývの代わりにLv。私の悪い。
ケビンクルーッセン

0

C(gcc)229 220 216バイト

ceilingcatのおかげで-9バイト。

ゼロインデックス。座標を数値のリストとして取得します。偶数はX、奇数はYです。

X,Y,i,j,k,x,z;f(l,n)int*l;{for(X=Y=0,i=n*=2;i--;X=fmax(l[i],X))Y=fmax(l[i--],Y);n&&X++-Y++;for(--i;i++<Y;puts(""))for(j=-1;j<=X;z=i<0|i==Y,putchar(j++<0|j>X?z?43:'|':x?z?45:32:'x'))for(x=k=n;k--;)x*=l[k--]-i|l[k]-j;}

オンラインでお試しください!


@ceilingcat乾杯!
ガストロプナー

提案for(n&&X++-Y++;i<=Y;i+=puts(""))の代わりにn&&X++-Y++;for(--i;i++<Y;puts(""))
ceilingcat
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.