崩溃的艺术


12

挑战

给定一个可打印的ascii字符的矩形网格作为字符串或通过标准输入,编写一个将非空格字符折叠为底部的函数或程序。

规则:

  • 输出具有与输入相同的尺寸和字符。
  • 在非空格字符(row a,column b)不能有空格字符' '(a-1, b)(a-1,b-1)或者(a-1,b+1),当行从最底层向上编号。结果是所有垂直桩都应向侧面倒塌。
  • 非空格字符可以在大多数(initial height - final height)位置向左或向右移动(见图1)。
  • 您可以假定图片有足够的空间折叠而字符不会掉出屏幕。

图1:分别@#$显示为的字符的最终位置x,y,z

..............
...@..........
..xxx.........
.xxxxx...#....
xxxxxxx.yyy.$.

字符折叠的顺序可以自由选择。尾随空格不行,但尾随换行符是可以的。

这是,因此最短答案以字节为单位!

                 (__)
                 (oo)
           /------\/
          / |    ||
         *  /\---/\
            ~~   ~~
..."Have you mooed today?"...

一种可能的输出:

 
 
 
                --(_
           /----|/|(o_)
          /|/~\---~\\/o)
..."Have*you~mooed~today?"...

只是要澄清一下,字符掉落的方式可以是硬编码的,而不是每次都随机生成的?
ETHproductions 2016年

18
那头牛对你做了什么?:(
FlipTack

@ETHproductions是正确的。我的示例按照从下到上,从左到右的顺序执行,但是只要遵守规则,就可以采用随机顺序或其他方法。
2016年

@ Flp.Tkc这只是一个模型。
2016年

1
@DestructibleWatermelon如果某个角色碰巧掉出了屏幕,那是他们的错,而不是您的错。
2016年

Answers:


4

JavaScript(ES6),100 90 88字节

f=s=>s==(l=s.search`
`,s=s.replace(eval(`/(\\S)([^]{${l-1},${l+1}}) /`),` $2$1`))?s:f(s)
s=`                 (__)        
                 (oo)        
           /------\\/         
          / |    ||          
         *  /\\---/\\          
            ~~   ~~          
..."Have you mooed today?"...`
console.log(s)
console.log(f(s))

要求字符串具有至少两行,并且所有行填充为相等的长度。示例图像的输出:

              ( --           
            /|---/|-(o__     
          */~~\---~\|\/o))   
..."Have you/mooed~today?"...

请注意,在可能的情况下,它会尝试将元素向右移动,*因此不会落在Have和之间you

编辑:由于@ETHproductions,节省了10%。由于@DanielIndie,又节省了2个字节。

视网膜0.8.2,50字节

+`(?<=(.)*)(\S)(.*¶(?<-1>)?(?>(?<-1>.)*).?) 
 $3$2

在线尝试!我的JavaScript回答的方法略有不同,它使用一个平衡组来匹配非空格字符下方的空格;在(?<-1>)?允许的空间是一列到左边,而.?允许的空间是一列在右边。

视网膜,40字节

~0L$`.(.*)¶
+s`(\S)(.{$.1,$.&}) ¶ $$2$$1

在线尝试!我的JavaScript回答的端口。的0L$原子级获取输入和两个长度代入导致实际执行替换,然后将其在由原始输入评估该命令的第二行~化合物的阶段。


这是一个很棒的算法!您可以\n用文字换行符代替,以将其降低到两位数;-)
ETHproductions 2016年

另外,我认为您可以这样l=s.search`\n` 节省一些字节。
ETHproductions 2016年


@DanielIndie f=递归是必需的,但是它仍然为我节省了2个字节,谢谢!
尼尔,

你是对的,抱歉:P
DanielIndie

3

Python 2,298字节

a=input()
L=len(a);s=' '
a=[list(s*L+l.ljust(L+max(map(len,a))))for l in a]
t=1
while t:
 t=0
 for y in range(L-1):
  for x in range(len(a[y])):
   c=a[y][x];C=a[y+1][x-1:x+2]
   if s!=c and s in C:t=1;a[y][x]=s;a[y+1][[[x+1,x][C[1]==s],x-1][C[0]==s]]=c
for l in map(''.join,a):print l[L:].rstrip()

将输入作为字符串列表(每行一个)

示例:输入:

['                 (__)',
'                 (oo)',
'           /------\/',
'          / |    ||',
'         *  /\---/\ ',
'            ~~   ~~',
'..."Have you mooed today?"...']

输出:

              (
            -----/|-(o__
         //|~~\---~\|\/o))
..."Have*you/mooed~today?"...

3

C,252字节

e=1,l,c,i,j,p,r,w,a[999];f(){while((i=getchar())>0)a[w++]=i,i<16?l++:0,l?0:c++;while(e)for(i=e=0;i<c;i++)for(j=l;j>=0;j--)e=(r=a[p=j*(c+1)+i]-32?a[r=p+c+1]-32?a[r=p+c]-32?a[r=p+c+2]-32?0:r:r:r:0)?l=a[p],a[p]=a[r],a[r]=l:e;for(i=0;i<w;)putchar(a[i++]);}

取消测试代码:

#include <stdio.h>

e=1,l,c,i,j,p,r,w,a[999];
f()
{
    // counting lines and columns
    while ((i = getchar())>0)a[w++] = i, i<16 ? l++ : 0, l ? 0 : c++;
    // main shaking loop
    while (e) // repeat while collapsing
        for (i = e = 0; i < c; i++) // columns loop
            for (j = l; j >= 0; j--) // lines loop
                e = ( // remember that collapsing was
                     r = // find place to collapse
                         a[p = j*(c + 1) + i] - 32 ? // if not space
                             a[r = p + c + 1] - 32 ? // if char under the current is not a space
                                 a[r = p + c] - 32 ? // see one position left
                                    a[r = p + c + 2] - 32 ? 0 // then one position right
                                                          : r
                                    : r
                                 : r
                             : 0
                         ) ? // and if place was found
                           l=a[p],a[p]=a[r],a[r]=l // replace values in positions p and r
                           : e;
    //print resulting picture
    for(i=0;i<w;)putchar(a[i++]);
}

int main(void)
{
    int cnt;
    FILE * testf = fopen("caw.txt","w");
    char testd[][31] = {
        "                 (__)        \n",
        "                 (oo)        \n", 
        "           /------\\/         \n", 
        "          / |    ||          \n", 
        "         *  /\\---/\\          \n", 
        "            ~~   ~~          \n", 
        "...\"Have you mooed today ? \"...",
        "" };
    // prepare data for test
    printf("Initial data:\n");
    for(cnt = 0; cnt < 7; cnt++)
    {
        printf("%s", testd[cnt]);
        fprintf(testf, testd[cnt]);
    }
    fclose(testf);
    // redirect standard input
    freopen("caw.txt", "r", stdin);
    printf("\n\nResult:\n");
    // start test
    f();
}

测试结果:

在此处输入图片说明


2

Algodoo(非竞争)

输入-使用简并的​​示例。

设定

跑步-默认重力和弹跳。

跑步

输出-可以通过对象上的“摩擦”和“密度”设置来调整精度。

输出量

Algodoo是逻辑可编程的


为什么这种不竞争?通常,不竞争是为使用比挑战更新的语言提供的答案。
Ad Hoc Garf Hunter,2013年

尽管Algodoo可以执行真实的逻辑,但是此模拟等效于在地板上丢弃输入的切碎打印输出并对其进行拍照。我不确定如何以编程方式捕获该输出。
wyldstallyns

说“零字节程序!我赢了!”似乎是错误的。
wyldstallyns

我怀疑这实际上不是零字节的答案。您谈论的调整设置似乎等同于编写代码。提出有关计分Algodoo的元问题可能是个好主意。我也认为零字节程序没有任何问题。
Ad Hoc Garf Hunter,2013年

我将打开该元数据。
wyldstallyns

1

JavaScript,286字节

b=>eval('f=b=>b==null||" "==b;b=b.split`\n`.map(b=>[...b]);a:for(;;){for(c=0;c<b.length-1;c++)for(g=b[c],d=0;d<g.length;d++){h=g[d];if(!f(h)){e=0;f(b[c+1][d])?e=2:f(b[c+1][d-1])?e=1:f(b[c+1][d+1])&&(e=3);if(e){b[c+1][d+e-2]=h;b[c][d]=" ";continue a}}}break}b.map(b=>b.join``).join`\n`')

例子

// Here I assume that you've assigned the above function to `fall`
console.log(fall(`
                 (__)
                 (oo)
           /------\/
          / |    ||
         *  /\---/\\
            ~~   ~~
..."Have you mooed today?"...`))

输出:

                -       
            /--(-\--(__  
          /|~~---/~||/oo))
..."Have*you/mooed~today?"...

另一个例子:

console.log(fall(`
 (\__/)  .~    ~. ))
 /O O  ./      .'
{O__,   \    {
  / .  . )    \\
  |-| '-' \    }
 .(   _(   )_.'
'---.~_ _ _&`))

输出:

    _ , /            
  OO/__'_.. .         
 {.(|-|.(O'))/.~{      
/('---.~___-_&)_.'}\~.'))

非高尔夫功能

function fall(input) {
  let move = true
  let lines = input.split("\n").map(line => line.split(""))
  let isSpace = c => c == null || c == " "
  loop: for (;;) {
    for (let y = 0; y < lines.length - 1; y++) {
      let line = lines[y]
      for (let x = 0; x < line.length; x++) {
        let ch = line[x]
        if (!isSpace(ch)) {
          let dx = 0
          if (isSpace(lines[y+1][x])) { dx = 2 }
          else if (isSpace(lines[y+1][x-1])) { dx = 1 }
          else if (isSpace(lines[y+1][x+1])) { dx = 3 }
          if (dx) {
            lines[y + 1][x + dx - 2] = ch
            lines[y][x] = " "
            continue loop
          }
        }
      }
    }
    break
  }
  return lines.map(line => line.join("")).join("\n")
}

一种测试非空白的简短方法是c>" ",其中c代表要测试的字符。
尼尔
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.