整体建筑有多高?


29

这是一个整体输入的示例。此示例中有4个。

  _
 | |        _
 | |  _    | |
 | | | |   | |     _
_| |_| |___| |____| |_

第一个整体为4个单元,第二个为2,第三个为3,最后一个为1。

任务

您的程序应按从左到右的顺序输出整体的高度。输出格式可以是任何类型的列表或数组。

笔记

  • 输入可以视为任何维字符串,字符串列表或字符列表。
  • 这是,因此最低字节获胜
  • 您应假定整料始终具有相同的宽度,并且彼此之间的_距离始终至少为1 。
  • 它们可以以任何高度,任何数量出现。

输入输出

  _
 | |        _
 | |  _    | |
 | | | |   | |     _
_| |_| |___| |____| |_   >> [4,2,3,1]

           _
          | |
  _       | |
 | |  _   | |  _
_| |_| |__| |_| |_   >> [2,1,4,1]


 _   _   _ 
| |_| |_| |_____   >> [1,1,1]

____________________   >> undefined behavior

 _
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |   >> [11]

     _       _       _       _       _
 _  | |  _  | |  _  | |  _  | |  _  | |
| |_| |_| |_| |_| |_| |_| |_| |_| |_| |  >> [1,2,1,2,1,2,1,2,1,2]

2
我可以假设输入正确地用空格填充了吗?
isaacg

17
你的[10]巨石不是[11]吗?
TessellatingHeckler

未定义的不是只是一个空数组吗?
所罗门·乌科

@isaacg是的,这将是好的
引力

@SolomonUcko从技术上讲是可以的,尽管为了简化所有语言的使用,我还是决定不让它们处理它。
重力1998年

Answers:


15

果冻(8?)9 字节

Ỵ=”|Sḟ0m2

单子链接,接受指定的字符列表并返回整数列表。

注意:如果一串字符串(每行一个)确实是允许的输入格式,则为8个字节 -只需删除

在线尝试!

怎么样?

Ỵ=”|Sḟ0m2 - Link: list of characters, s
Ỵ         - split at newlines
  ”|      - literal '|'
 =        - equals (vectorises)
    S     - sum (vectorises, hence counts the number of '|' in every column)
     ḟ0   - filter out zeros (only keep the results from the sides of the towers)
       m2 - modulo index with 2 (keep only the left side measurements)

我不知道,但这个好吗?
V. Courtois

1
@ V.Courtois我不明白为什么不这样做,因为我们可能永远也不会得到这样的输入。
暴民埃里克(Erik the Outgolfer)

好的,这是因为我看到其他一些答案也算在内
V. Courtois

2
@ V.Courtois您建议的输入不符合规范,类似地,在天空中添加下划线,在整体结构中部分向上或在地下添加可能会破坏许多其他提交。
乔纳森·艾伦,

不满意-您是否愿意说明原因?
乔纳森·艾伦,


6

JavaScript(ES6),79 78字节

-1字节感谢@Shaggy

a=>a.map((b,i)=>b.replace(/_/g,(_,j)=>o[j]=a.length-i-1),o=[])&&o.filter(x=>x)

将输入作为字符串数组。

测试片段

f=
a=>a.map((b,i)=>b.replace(/_/g,(_,j)=>o[j]=a.length-i-1),o=[])&&o.filter(x=>x)
I.value="           _\n          | |\n  _       | |\n | |  _   | |  _\n_| |_| |__| |_| |_"
<textarea id=I rows=7 cols=30></textarea><br><button onclick="O.value=`[${f(I.value.split`\n`).join`, `}]`">Run</button> <input id=O disabled>


1
78个字节:a=>a.map((x,y)=>x.replace(/_/g,(_,z)=>c[z]=a.length-y-1),c=[])&&c.filter(n=>n)
粗野的

@Shaggy Nice,我完全没想到要使用replace。谢谢!
贾斯汀·马里纳

6

C ++,171个 169字节

#import<vector>
#import<iostream>
int f(std::vector<std::string>s){for(int a,j,i=0,k=s.size()-1;a=s[k][i];++i)if(a==32){for(j=0;(a=s[k-++j][i])-95;);std::cout<<j<<" ";}}

在线尝试!

C ++(GCC),150字节

感谢@aschepler!

#import<vector>
#import<iostream>
int f(auto s){for(int a,j,i=0,k=s.size()-1;a=s[k][i];++i)if(a==32){for(j=0;(a=s[k-++j][i])-95;);std::cout<<j<<" ";}}

在线尝试!


1
如果使用g ++,则可以使用非标准,f(auto s)并指定它采用的随机访问容器的任何随机访问容器char
aschepler '17


5

Dyalog APL,29个字节

{0~⍨↑+/(⌈/⍴¨⍵)↑¨(⍳≢⍵)×⌽⍵='_'}

用运行⎕IO←0

在线尝试!

怎么样?

⌽⍵='_'- ,其中'_',顶线首先

×- 乘以 ...

(⍳≢⍵)- (零索引)的范围

↑¨ -对于每行,用零填充...

(⌈/⍴¨⍵) -最大长度

↑+/ -将压缩后的行加总

0~⍨ -删除零



5

PowerShell,133个字节

param($s)$r=,0*($l=($s=$s-replace'\| \|',' 1 ')[0].Length);1..$s.Count|%{$z=$_-1;0..($l-1)|%{$r[$_]+=(''+$s[$z][$_]-as[int])}};$r-ne0

看起来竞争力不强;它进行了正则表达式替换,以将塔变成1列,使输入字符串长度为0的数组,然后逐步将各行加起来为1。

准备运行的测试:

$s1 = @'
  _                   
 | |        _         
 | |  _    | |        
 | | | |   | |     _  
_| |_| |___| |____| |_
'@-split"`r?`n"


$s2 = @'
 _
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | 
'@-split"`r?`n"

$s3 = @'
           _      
          | |       
  _       | |           
 | |  _   | |  _   
_| |_| |__| |_| |_ 
'@-split"`r?`n"


$s4 = @'
 _   _   _      
| |_| |_| |_____ 
'@-split"`r?`n"

$s5 = @'
     _       _       _       _       _ 
 _  | |  _  | |  _  | |  _  | |  _  | |
| |_| |_| |_| |_| |_| |_| |_| |_| |_| | 
'@-split"`r?`n"

4

Japt,11个字节

z ·mb'_ fw0

在线测试!

说明

z ·mb'_ fw0   : Implicit input
z             : Rotate the input clockwise. This puts the "floor" against the left side.
  ·           : Split the 2D string into lines.
   m          : Replace each column (now row) X with
    b'_       :   the index of '_' in X (0-indexed). This gives us the output list, with
              :   0's and -1's mixed in representing the columns that are not monoliths.
        f     : Take only the items X where
         w0   :   max(X, 0) is truthy. Since 0 is falsy, this removes anything <= 0.
              : Implicit: output result of last expression

4

视网膜48 38字节

^
¶
{`(¶.*)*¶_(.*¶)+
$#2 $&
}`¶.
¶
G`.

在线尝试!链接包括第一个示例。说明:一行是前缀,它将收集结果。依次重复删除每列时,那些包含_地上高度的列将计算该列中剩余的行数。最后,现在空白行被删除。编辑:由于@FryAmTheEggman的侵害,节省了10个字节。


很好,我的解决方案略短一些,但不适用于大型输入,因为这会打乱我对它们进行排序的能力。逐列浏览是避免这种情况的好方法!
FryAmTheEggman

@FryAmTheEggman我切换到了使用_s 计算行数的方法,这比尝试使用|s 有意义得多,谢谢!
尼尔

@FryAmTheEggman不能解决问题,但是可以通过以下方式简化排序阶段:将lookbehind放到后面并进行排序$.%`,最后一个阶段可以是!`\d+。并且,如果您将第一阶段更改为前瞻,则无需循环。
Martin Ender

@FryAmTheEggman 是您的方法的一种修复,但最终以46个字节结尾。
Martin Ender

@MartinEnder 45也许?在线尝试!
尼尔

4

爪哇8,133个 117 116 114字节

a->{for(int l=a.length-1,i=0,j;i<a[0].length;i++)if(a[l][i]<33){for(j=0;a[j][i]<33;j++);System.out.print(l-j+",");}}

将输入作为(←保存16个字节)。 -2字节换取较少可读的输出由于@OlivierGrégoire通过改变到。String[] char[][]
print(l-j+",")println(l-j)

说明:

在这里尝试。

a->{                         // Method with character 2D-array parameter and no return-type
  for(int l=a.length-1,      //  Length of the 2D char-array - 1
      i=0,j;                 //  Index-integers
    i<a[0].length;i++)       //  Loop (1) over the 2D char-array
    if(a[l][i]<33){          //   If the base of the current column is a space
      for(j=0;a[j][i]<33;    //    Loop (2) over the cells in this column as long as
                             //    we encounter spaces (from top to bottom)
        j++                  //     And increase `j` every time, to go down the column
      );                     //    End of loop (2)
      System.out.println(l-j);
                             //    Print the amount of rows - `j`
    }                        //   End of if-block
                             //  End of loop (1) (implicit / single-line body)
}                            // End of method

我还没有尝试过,但是对于这个挑战,我的想法是从上到下查找_是否找到了存储的位置,然后在其上排序,而无视最后一行。可能有助于保存字节...
TheLethalCoder

@TheLethalCoder那也是我最初的想法,但是您想将它存储在/订购哪里?我本来以为是Map,但是这些是未分类的,因此您需要一个LinkedMap。在我的脑海中,这听起来似乎有点太多字节,但是如果您能找到一种比完成它更短的方法,可以随时发布答案,我将对其进行+1。:)
Kevin Cruijssen

我只设法使用Linq将其降低到150,但是仍然应该有打高尔夫球的空间。
TheLethalCoder

在C#中,我们具有多维数组,例如:您new[,]不是将锯齿状数组用作new[][]。如果您在Java中使用它,可以节省一些字节。
TheLethalCoder

1
System.out.println(l-j);看起来足够让我节省2个字节。另外,在说明中,您忘了将其更改length()length(提交中正确的字节数不受影响)。
OlivierGrégoire'7

3

哈斯克尔, 75 74字节

import Data.List;f=filter(>0).map(length.fst.span(<'!').reverse).transpose

该输入应作为字符串列表(行)。


当换行符的长度相同且惯用时,为什么还要在导入后使用分号?
Jules

@Jules:是的,我通常会这样做
siracusa




3

C#,150个 144 137字节

using System.Linq;a=>a.SelectMany((i,h)=>i.Select((c,w)=>new{c,w,d=a.Length-1-h}).Where(o=>o.c==95&o.d>0)).OrderBy(o=>o.w).Select(o=>o.d)

完整/格式化版本:

using System;
using System.Collections.Generic;
using System.Linq;

class P
{
    static void Main()
    {
        Func<char[][], IEnumerable<int>> f = a =>
            a.SelectMany((i, h) => i.Select((c, w) => new { c, w, d = a.Length - 1 - h })
                                    .Where(o => o.c == 95 & o.d > 0))
             .OrderBy(o => o.w)
             .Select(o => o.d);

        Console.WriteLine(string.Concat(f(new char[][]
        {
            "  _                 ".ToArray(),
            " | |       _        ".ToArray(),
            " | |  _   | |       ".ToArray(),
            " | | | |  | |    _  ".ToArray(),
            "_| |_| |__| |___| |_".ToArray(),
        })));

        Console.ReadLine();
    }
}

3

Java 8-229字节 213字节

s->{Map<Integer,Integer> m=new TreeMap();String[] l=s.split("\n");for(int i=0,j=-1;i<l.length-1;++i){s=l[i];while((j=s.indexOf("_",j+1))>=0){m.put(j,i);}}for(int i:m.values()){System.out.print(l.length-i-1+",");}}

在线尝试!

取消高尔夫:

public static void foo(String input)
{
    Map<Integer, Integer> map = new TreeMap(); // Raw types!!
    String[] lines = input.split("\n");

    for (int i = 0, j = -1; i < lines.length - 1; ++i)
    {
        input = lines[i];

        while ((j = input.indexOf("_", j + 1)) >= 0)
        {
            map.put(j, i);
        }
    }

    for(int i:map.values())
    {
        System.out.print(lines.length - i - 1 + ",");
    }
}

oo,第一篇文章。改善它的任何帮助都会很棒。知道我可以摆脱indexOf两次写的那个。知道!我曾想过将地图中的类型从Integer更改为Long的想法,但我认为那是死路一条。


我知道已经有很多更好的Java 8解决方案,但是char[][]我认为在这种情况下,它比String更容易作为输入。


1
您尚未包括导入(Java答案是必需的)。请勿使用Map而是int[](可能已初始化为new int[99]?)。之后的空格String[] lString[]l相同,但更短。使用println(l.length-i-1)代替println(l.length-i-1+",")。不要初始化j:只需编写:,j;。如果您int[]按照前面的建议使用a ,请像这样声明:int m[]=new int[99],i=0,j;并从中删除该声明for-loop
奥利维尔·格雷戈尔

1
OlivierGrégoire对于Map所需的导入确实是正确的。至于使用Map浏览当前代码的方法,您可以将其更改为:import java.util.*;s->{Map m=new TreeMap();String[]a=s.split("\n");int l=a.length-1,j=-1,i=j;for(;++i<l;)for(s=a[i];(j=s.indexOf("_",j+1))>=0;m.put(j,i));for(Object o:m.values())System.out.println(l-(int)o);}<Integer,Integer>您可以投射到时不需要地图的inta.length-1被使用两次,因此您可以为其使用变量;通过将所有内容放入for循环中,您可以摆脱所有括号。哦,欢迎来到PPCG!:)
凯文·克鲁伊森

@KevinCruijssen谢谢!将第一个for循环的内容转换为无主体的for循环令人鼓舞!超级聪明。
迈克尔”

@Michael,不客气。:)哦,如果您还没有看到它的话,那么通读Java 技巧<all language>技巧可能会很有趣。刚开始时对我有很大帮助(有时还是有帮助)。
凯文·克鲁伊森


2

Mathematica,48 47 39字节

Last/@(Reverse@Most@#~Position~"_")&

在线尝试!

Function需要一个矩形的字符数组。取Most数组(最后一行除外),Reverse然后取Transpose*,然后找到出现字符的所有Positions _。相关的高度是Last每个元素的要素Position

* 是在Mathematica 中表示的3字节专用字符。请注意,这在Mathics不起作用,因此TIO链接仅使用。U+F3C7\[Transpose]Transpose


2

SOGL V0.12,9个字节

I{ _WH╥?O

在这里尝试!
将输入作为字符串(字符)数组的数组。

说明:

I          rotate the array clockwise
 {         for each element
   _       push "_"
    W      get its index in the array (0 if not found, 1 if its the ground, >1 if its what we need)
     H     decrease that
      ╥    palindromize (duplicates the number, if it's <0, then errors and pushes 0, if =0, pushes 0, if >0, then pushes the number palindromized (always truthy))
       ?   if that, then
        T  output in a new line the original decreased index

2

JavaScript(ES6),108 104 88字节

@JustinMariner节省了16个字节

i=>i.map((s,h)=>{while(t=r.exec(s))m[t.index]=i.length-h-1},m=[],r=/_/g)&&m.filter(e=>e)

输入为字符串数组

let input = [
'  _',
' | |           _',
' | |  _   _   | |',
' | | | | | |  | |     _',
'_| |_| |_| |__| |____| |_'
]

let anonymousFunction =
i=>i.map((s,h)=>{while(t=r.exec(s))m[t.index]=i.length-h-1},m=[],r=/_/g)&&m.filter(e=>e)

console.log(anonymousFunction(input))



谢谢@JustinMariner!我为变量初始化作为给的未使用参数而感到困惑Array.map,这是一个很酷的技巧。
alexanderbird

您是否真的需要将RegEx分配给变量?您可以直接在中使用它exec并保存一些字节。
毛茸茸的

实际上,这是必要的-while循环会在一行中的每个匹配项上进行迭代,并且在变量中不存在正则表达式的内部状态的情况下,它将每次都匹配首次出现并无限循环。每次迭代都会创建一个新的正则表达式,因此exec将与第一个匹配。如果您内联正则表达式,实际上会使堆栈交换代码段编辑器崩溃。除非我想念什么?
亚历山大·

@shaggy我忘了在我的最后评论中给您加上标签
亚历山大

2

CJam,15个 14字节

@BusinessCat节省了1个字节

{W%z'_f#{0>},}

这是一个在堆栈上获取字符串数组并输出数组的块。

说明:

W%    e# Reverse
z     e# Zip
'_f#  e# Get the index of '_' in each element (-1 if not found)
{0>}, e# Filter where positive

您可以通过在转置之前反转整个数组来节省1个字节。
Business Cat

1

18 17字节

15个字节的代码,-rp标志+2 。

_FI_@?'_MRVgZDs

接受来自stdin的输入。在线尝试!

说明

                 g is list of lines from stdin (-r flag); s is space
         RVg     Reverse g
            ZDs  Zip (transpose), filling gaps with a default char of space
        M        Map this function:
   _@?'_          Index of _ in each line (or nil if it doesn't appear)
_FI              Filter, keeping only the truthy (nonzero, non-nil) values
                 Autoprint in repr format (-p flag)

1

Pyth19 15 14字节

f>T0mx_d\_.tQd

在线测试!输入是行列表。

说明

          .tQd     # Transpose, pad with spaces
    mx_d\_         # For each line, reverse it, find the position of "_" (-1 if not found)
f>T0               # Filter on positions greater than zero


1

Perl 6、65字节

{m:ex/^^(\N+)_([\N*\n]+:)/.sort(*[0].chars).map(+*[1].comb("
"))}

在线尝试!

  • m:exhaustive/^^(\N+)_([\N*\n]+:)/在输入字符串中搜索所有下划线,并为每个下划线返回一个匹配对象,其中第一个捕获括号包含找到下划线的行的前一部分,第二个捕获括号包含字符串的其余全部。字符串的其余部分必须至少包含一个换行符,因此我们不算底线的下划线。该:exhaustive标志允许这些匹配重叠。
  • .sort(*[0].chars)根据每个下划线前一行中的字符数对这些匹配对象进行排序。这命令他们从左到右。
  • .map(+*[1].comb("\n"))将每个匹配对象映射到输入字符串中每个下划线之后的部分中的换行字符数,即高度。该\n是一个实际的换行符,节省了一个字节。

0

PHP,119字节

function($s){$r=array_map(null,...$s);foreach($r as$k=>&$v)if($v=array_count_values($v)['|'])echo($v+$r[$k+2]=0)." ";};

让我们分解一下!我们在这里输入的是2D字符数组。

$r=array_map(null,...$s) // Neat little snippet to transpose the array

foreach($r as$k=>&$v)    // Loop through the array, grabbing each row of our 2D array 
(which is now each column of the monolith)

if($v=array_count_values($v)['|']) // Count the number of '|' characters in the column 
(which is the height of our monolith), and if it's greater than 0 (truthy in PHP)...

echo($v+$r[$k+2]=0)." "; // Output that number, and simultaneously set the row 2 indices
                            down to null (to remove duplicate values)

-1

接受多行字符串。设置(页眉和页脚)的信誉转到@GarethPW

Python 2,29个字节

lambda s:len(s.split('\n'))-1

在线尝试!

这将简单地通过换行符分割数组并返回length-1。


这不能解决整个挑战。您必须返回具有所有高度的数组或列表,而不仅仅是所有高度。
Scott Milner
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.