藏宝图绘图机器人


14

您正在为朋友组织寻宝活动。为了更轻松地进行操作,您想要绘制隐藏贵重物品的所有位置的地图。

输入值

0 0允许使用任何形式的输入来表示由(负)x和y坐标组成的点列表,即左上角(注意:您也可以在答案中使用基于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 '18

可以有非正方形地图吗?
FrownyFrog

4
@ racer290我建议您说些什么the input is a list of locations (e.g. nested list, list of tuples, space & newline separated, separate inputs, ect.)
dzaima '18

1
输出可以是二维字符数组吗?
ovs '18

2
我可以提交将x坐标和y坐标作为两个独立参数的函数吗?
ბიმო

Answers:


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 '18

为什么::empty这么冗长?怎么办 为什么不能将其简化为1个字节左右?(我对J不了解)
魔术章鱼缸

我在没有:: empty的情况下在TIO上运行它,它似乎也起作用(也不知道J)
Quintec

实际上:: empty似乎可以处理“”输入案例
Quintec '18

@MagicOctopusUrn我不知道输出真正空盒子的更短方法,默认情况下它们是1行高。
FrownyFrog

4

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

在线尝试!


4

Haskell127123字节

这定义了运算符(!),该运算符采用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]

在线尝试!

取消高尔夫/解释

helper函数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]

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

Python 2中151个 140 138字节

-2个字节,感谢Jo King。

输入为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 '18

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)环绕花费ab从堆栈中,并产生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@

通过[<-直接使用大括号删除122个字节
朱塞佩

确实是@Giuseppe!我知道一定有办法。
JayCe

1

坐标采用[y,x]格式

JavaScript(Node.js)191184字节

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您能更具体一点吗?
DanielIndie

尝试您的解决方案时,我发现更改测试用例中的x坐标会导致该坐标的垂直方向发生变化。我想这个错误在第一行(a.map(([y,x])
racer290 '18

但是x是正确的参数,如测试用例所示
-DanielIndie

2
那么在您的解决方案中,您首先要使用y坐标吗?我认为最好在答案中对此做个注释。
racer290 '18

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; 卫生?; forwhxxwr。foryyhrxwyh?xwyhcxy?xr。
魔术章鱼缸

@MagicOctopusUrn您为所有变量和get/ 命名的for是什么?:S XD
Kevin Cruijssen

1

C(gcc)246234字节

多亏了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';}

在线尝试!


修复底部行235个字节中的
1919年

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字节

-9字节归功于ceilingcat

零索引。将坐标作为数字列表,其中偶数是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干杯!
gastropner '18 -10-19

建议for(n&&X++-Y++;i<=Y;i+=puts(""))不要使用n&&X++-Y++;for(--i;i++<Y;puts(""))
ceilingcat '19
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.