将ASCII艺术带入三维


11

在这个挑战中,您必须将ASCII艺术(通常是2D)带入3D!

怎么样?

像这样,

X X DD 
 X  D D
X X DD 

至...

  X X DD 
 X X DD D
X X DDDD 
 X XDDD
X X DD

那我们怎么做呢?

给定ascii艺术和N,重复N一次。

  • 对于每个字符(我们将其称为A):
  • B成正好是1右1的字符A
  • 如果B是空格或未定义:
  • 设置BA

眼镜

  • 第一个输入可以是带有换行符的字符串,也可以是代表2D ASCII艺术的字符串列表。
  • 您可以使用%END%标记输入的结尾,但这不是必需的。
  • 第二个输入将是N。这将是一个正整数。
  • 字符串的所有行都将具有相同的长度。

例子

输入: ("###\n###",1)

输出:

 ###
####
###

规则

基本的规则适用。

另外,如果您有任何疑问,请务必在评论中提问!


您可能需要澄清“空白”是指空格(U + 0020)或什么都没有。
Leaky Nun

@LeakyNun现在修复了吗?

是否允许多余的空格?
Leaky Nun

是的,那些是允许的。

1
我可以假设每行的长度都一样吗?(我可以在右边的空格处预填充输入吗?)
Leaky Nun

Answers:


18

Perl,81个字节

75字节代码+ 6 for -i -n0
请注意,这些\e字符是ASCII,\x1b\e用于简化测试。

请注意,此解决方案使用ANSI转义序列,并且需要兼容的终端,以及使用-i命令行参数来传递所需的“尺寸”数。

$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I

用法:

在Linux兼容终端中,PS1=首先运行以确保您的提示不会覆盖显示的图像。

perl -i10 -n0e '$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I' <<< ' 
ROFL:ROFL:ROFL:ROFL
         _^___
 L    __/   [] \    
LOL===__        \ 
 L      \________]
         I   I
        --------/
'

          ROFL:ROFL:ROFL:ROFL
         ROFL:ROFL:ROFL:ROFL
        ROFL:ROFL:ROFL:ROFL
       ROFL:ROFL:ROFL:ROFL\
      ROFL:ROFL:ROFL:ROFL\_]
     ROFL:ROFL:ROFL:ROFL\_]
    ROFL:ROFL:ROFL:ROFL\_]/
   ROFL:ROFL:ROFL:ROFL\_]/
  ROFL:ROFL:ROFL:ROFL\_]/
 ROFL:ROFL:ROFL:ROFL\_]/
   LOL==___^___]_\_\_]/
  LOL==__/ \_[]_\_\_]/
 LOL===__ \______\_]/
  L      \________]/
          I---I---/
         --------/

perl -i3 -n0e '$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I' <<< 'X X DD
 X  D D
X X DD
'
   X X DD
  X X DD D
 X X DDDD
  X XDDD
 X X DD

8

CJam,25个 24字节

{{' 1$f+La@+..{sS@er}}*}

一个未命名的块,需要一个字符串列表和堆栈上的重复次数,并保留一个新的字符串列表。

在这里测试。(为方便起见,包括一个测试包装器,该包装器从STDIN读取字符串。)

说明

{       e# Repeat this block N times...
  '     e#   Push a space character.
  1$    e#   Copy the current grid.
  f+    e#   Prepend the space to each line of the grid.
  La    e#   Push [[]].
  @+    e#   Pull up the other copy of the grid and prepend the [].
        e#   We've now got two copies of the grid, one shifted right by
        e#   a cell and one shifted down by a cell. We now want to replace
        e#   spaces in the latter with the corresponding character in the
        e#   former.
  ..{   e#   For each pair of characters in corresponding positions...
    s   e#     Turn the character in the down-shifted grid into a string.
    S   e#     Push " ".
    @   e#     Pull up the character from the right-shifted grid.
    er  e#     Replace spaces with that character.
  }
}*

5
十架直升机!goo.gl/PEK4iB

为什么S初始空间不起作用?此外,是否允许在函数中使用变量(可能已被覆盖)?
Luis Mendo

@LuisMendo S不起作用,因为那样f会在该字符串上进行映射。我相信,在“正常”语言中,关于函数的调用也有许多依赖全局变量的函数提交,它们在两次调用之间不会被篡改。
Martin Ender

谢谢。我忘记了CJam中的字符与一个字符的字符串不同
Luis Mendo

1
您可以使用Convex来保存一个字节,因为它具有一个1字符的音译运算符而不是一个2字符:凸.tryitonline.net /…(无耻插件)
GamrCorps

4

APL,49个字节

{⎕UCS 32⌈{s+(s=0)×1⊖¯1⌽s←0⍪⍵,0}⍣⍺⊣(32∘≠×⊣)⎕UCS↑⍵}

输入:字符向量的向量。例:

      2 {⎕UCS 32⌈{s+(s=0)×1⊖¯1⌽s←0⍪⍵,0}⍣⍺⊣(32∘≠×⊣)⎕UCS↑⍵} 'X X DD' ' X  D D' 'X X DD'
  X X DD 
 X X DD D
X X DDDD 
 X XDDD  
X X DD   

这个怎么运作:

  • ↑⍵ 将参数转换为字符矩阵
  • ⎕UCS 从字符到整数
  • (32∘≠×⊣) 用零代替空格(32)
  • ...⍣⍺⊣ 将⍺(左参数)乘以左函数
  • s←0⍪⍵,0 在参数的顶部和右侧带有零的边框
  • 1⊖¯1⌽ 向上旋转1向右旋转1
  • s+(s=0)× 将原始版本与原始版本相加,但仅在原始版本的零之上
  • 32⌈ 将零变成32s
  • ⎕UCS 从整数到char

4

MATL,24字节

:"ct32>*TTYatFTEqYSy~*+c

输入格式为

2
{'X X DD', ' X  D D', 'X X DD'}

所以另一个例子是

1
{'###', '###'}

输出包含额外的空格,这是质询允许的。

在线尝试!


如果可接受2D字符数组作为输入(我问过OP两次...),则c可以删除第一个字符数组,因此23个字节

:"t32>*TTYatFTEqYSy~*+c

在这种情况下,输入格式为(所有字符串的长度均相等,这可能需要右填充空格):

2
['X X DD '; ' X  D D'; 'X X DD ']

在线尝试!


说明

:        % Input number n implicitly. Generate [1 2 ... n]
"        % For loop: repeat n times
  c      %   Convert to char array. In the first iteration it inputs a cell array of
         %   strings implicitly and converts to a 2D char array, right-padding with
         %   spaces. In the next iterations it does nothing, as the top of the stack
         %   is already a 2D char array
  t32>*  %   Convert characters below 32 into 0
  TT     %   Push array [1 1]
  Ya     %   Pad the 2D char array with one zero in the two directions (up/down,
         %   left/right), on both sides
  t      %   Duplicate
  FTEq   %   Push array [-1 1]
  YS     %   Circularly shift the 2D char array one unit up and one unit right
  y      %   Push a copy of the non-shifted 2D array
  ~      %   Logical negate: nonzero entries become 0, zero entries become 1. This
         %   will be used as a mask for entries that need to be changed. Since the
         %   values at those entries are zero, we can simply add the new values. We do
         %   that by multiplying the mask by the shifted array and adding to the
         %   non-shifted array
  *      %   Multiply element-wise
  +      %   Add element-wise
  c      %   Convert the 2D array back to char
         % End for
         % Implicitly display

3

凸面,23字节

字节数假定为CP-1252编码。

{{' 1$f+La@+..{sS@Ë}}*}

一个未命名的块,需要一个字符串列表和堆栈上的重复次数,并保留一个新的字符串列表。

在线尝试!

这是我的CJam对Convex(主要基于CJam)的答案的直接端口。唯一的不同是Convex Ë代替er音译使用,节省了一个字节。感谢GamrCorps,让我知道了这一点。


2

Pyth,54 33字节

ju+++dhG.bsmh|-d;;.t,Y+dNdtGGeG.*

测试套件。


为什么需要两个;
2016年

@ven 在编程语言中;并不常见;
Leaky Nun

;是一个变量。
Leaky Nun

啊pyth重载;在lambda表达式...
法师

@ven当您与Pyth相处,你会用I.?VF;,(明确的语句)非常少,它们将被替换?muFMLR#,...
漏嫩

2

JavaScript(ES6),128个字节

f=(a,n)=>n?f((a=[``,...a].map(s=>[...s||` `])).map((b,i)=>i--&&b.map((c,j)=>a[i][++j]>' '?0:a[i][j]=c))&&a.map(b=>b.join``),n-1):a

接受并返回一个字符串数组,为输出添加一个额外的行,确保每行至少包含一个空格,将它们全部分割为字符,循环执行,尽管尝试将字符复制到上方的行和右侧的列,然后递归调用自身以完成循环。


2

Python 2,116字节

S=' '
def f(a,d):e=[S*len(`a`)];exec"a=[''.join(t[t[1]>S]for t in zip(S+x,y+S))for x,y in zip(a+e,e+a)];"*d;return a

我会再打高尔夫。


您确定a lambda不短吗?
wizzwizz4 '16

我需要e在那里的作业。另外,它exec是一个语句,因此不能放在lambda中。
林恩

好。只是确定一下。
wizzwizz4 '16

2

Ruby,95个字节

->a,n{n.downto(0){|i|f="<Esc>[1C"
$><<a.gsub(/^/,f*i).gsub(" ",f)+(i>0?"<Esc>[#{a.lines.size-1}A":"")}}

每个<Esc>都是文字ESC字符(0x1b)。

用法

将lambda分配给变量,例如func

art = <<END
X X DD
 X  D D
X X DD
END

func[art, 2]
# Prints:
#   X X DD
#  X X DD D
# X X DDDD
#  X XDDD
# X X DD

不打高尔夫球

->(art, num) {
  num.downto(0) do |i|
    forward = "\e[1C"
    $> << art.gsub(/^/, forward * i).gsub(" ", forward) +
            (i > 0 ? "\e[#{art.lines.size - 1}A" : "")
  end
}

forward转义序列,\e[1C,移动光标向前(右)1空间和\e[<n>A移动它n线。基本上,此代码的作用是将“图层”从背面打印出来,用forward顺序替换空格,以避免用空格覆盖其他图层。


1
你不小心把f=之前->在golfed版本。将其删除-2个字节。
wizzwizz4 '16
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.