スタッキングブロック


15

特定のポイントにドロップするブロックのリストの入力が与えられた場合、結果の「タワー」の高さを出力します。

この課題を説明する最良の方法は、例です。入力は、nブロックを表す2n個の整数のリストになります。最初の整数はブロックのx位置であり、0インデックスが付けられ、2番目の整数はブロックの幅です。たとえば、の入力はブロックを表します(x座標は以下にラベル付けされます)。2 4

  ####
0123456789

ここで、入力がであるとしましょう2 4 4 6。つまり、x = 2で幅が4の1つのブロックと、x = 4で幅が6の1つのブロックです。

    ######
  ####

a。)ブロックは常にタワーの最上部から「ドロップ」し、b。)ブロックは決して「倒れる」ことはありません(つまり、常にバランスが保たれます)。したがって、の入力は以下を 2 4 4 6 12 1表します。

    ######
  ####      #

最後のブロックが「地面」に落ちていることに注意してください。

最終的な出力は、各x値から最大値までのタワーの最大高さになります。したがって、入力2 4 4 6 12 1は出力になります 0011222222001

    ######
  ####      #
0011222222001

入力は、空白/コンマ区切りの文字列、整数の配列、または関数/コマンドライン引数のいずれかとして指定できます。ブロック位置(x値)は常に整数0以上で、幅は常に整数1以上で、常に少なくとも1つのブロックがあります。

出力は、非数値文字で区切られた単一の文字列(例"0, 0, 1, ...")、すべての数字をリストした単一の文字列(例、 "001..."最大高さは9以下であることが保証されます)、または整数の配列として指定できます。

これはであるため、バイト単位の最短コードが優先されます。

テストケース:

In                                   Out
---------------------------------------------------------
2 4 4 6 12 1                         0011222222001
0 5 9 1 6 4 2 5                      1133333222
0 5 9 1 2 5 6 4                      1122223333
0 5 2 5 6 4 9 1                      1122223334
20 1 20 1 20 1                       00000000000000000003
5 5                                  000011111
0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 4  123456789999

入力を2タプルの配列として取得できますか?
リルトシアスト

@ThomasKwaいいえ、入力は1次元配列でなければなりません。
ドアノブ

回答:



6

Python 3、89

def f(a):
 h=[]
 while a:x,w,*a=a;h[:x+w]=(h+[0]*x)[:x]+[max(h[x:x+w]+[0])+1]*w
 return h

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

この関数は、整数のリストを受け取って返します。

def f(a):                       # input as list of integers
  h=[]                          # list of heights
  while a:                      # while there's input left
    x,w,*a=a;                   # pop first 2 integers as x and w

    h[:x+w]=                    # change the heights between 0 and x+w
      (h+[0]*x)[:x]+            # left of x -> unchanged but padded with zeros
      [max(h[x:x+w]+[0])+1]*w   # between x and x+w -> set to the previous max + 1

  return h                      # return the list of heights

2

ルビー、 88 87バイト

f=->i{o=[]
(s,l,*i=i
r=s...s+l
o[r]=[([*o[r]]+[0]).max+1]*l
o.map! &:to_i)while i[0]
o}

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

grcの答えに触発されましたが、別の言語で、わずかに短くなっています。

説明:

f=->i                        # lambda with parameter i, expects array of ints
{
    o=[]                     # output
    (
        s,l,*i=i             # pop start and length
        r = s...s+l          # range is used twice, so shorten it to 1 char
        o[r] =
            [(
                    [*o[r]]  # o[r] returns nil if out of bounds, so splat it into another array
                    +[0]     # max doesn't like an empty array, so give it at least a 0
            ).max+1]*l       # repeat max+1 to fill length
        o.map! &:to_i        # replace nil values with 0
    ) while i[0]             # i[0] returns nil when i is empty, which is falsy
    o                        # return o
}

1

APL、79バイト

{⊃{o←(z←(≢⍵)⌈a←+/⍺)↑⍵⋄e←(z↑(-a)↑⍺[1]⍴1)⋄o+0⌈o-⍨e×e⌈.+e×o}/⌽(⊂⍬),↓(⌽2,0.5×≢⍵)⍴⍵}

APL配列としての入力、数字のAPL配列としての出力。


{⊃{o←⍵↑⍨z←(≢⍵)⌈a←+/⍺⋄e←z↑(-a)↑⍺[1]⍴1⋄o+0⌈o-⍨e×e⌈.+e×o}/⌽(⊂⍬),↓⍵⍴⍨⌽2,.5×≢⍵}(私の神、正しい使い方を学ぶ)
ザカリー

、あなたの言葉に注意してください...あなたは違いを知っていないようだ1↑、あなたは更新プログラムの原因間違った結果を与えるが、私はあなたをひいきにしていないことを示唆を与えるこれのために。
lstefano

ええ、私は得ることのように私はgolfedすることができ、物事の束を見たときに時々 。しかし、他のゴルフはまだ適用する必要があります。
ザカリー

彼らはすべてそうします。そして、できれば適切なクレジットであなたの提案を統合しました。
lstefano

-- やりましたか?- -0.5
ザカリー

0

Java 1.8、 1.8、351 329バイト

この最初の試みに興奮していません-ダブルループと、これらのInteger.valueOfのすべてをさらにゴルフできると確信しています。

interface B{static void main(String[]x){Byte b=1;int i=0,s,c,m=0,h,a=x.length,t[];for(;i<a;){s=b.valueOf(x[i++]);c=b.valueOf(x[i++]);m=m>s+c?m:s+c;}t=new int[m];for(i=0;i<a;){h=0;s=b.valueOf(x[i++]);c=b.valueOf(x[i++]);for(m=s;m<s+c;m++)if(t[m]>=h)h=t[m]+1;for(m=s;m<s+c;)t[m++]=h;}for(i=0;i<t.length;)System.out.print(t[i++]);}}

非ゴルフ

interface B {
static void main(String[]x){
    int start, count, maxWidth=0, height, args=x.length, totals[];
    Byte b=1;
    for (int i=0; i<args;){
        start = b.valueOf(x[i++]);
        count = b.valueOf(x[i++]);
        maxWidth = maxWidth>start+count ? maxWidth : start+count; 
    }
    totals=new int[maxWidth];
    for (int i=0; i<args;){
        height=0;
        start = b.valueOf(x[i++]);
        count = b.valueOf(x[i++]);
        for (int j = start; j<start+count; j++) {
            if (totals[j]>=height) {
                height=totals[j]+1;
            }
        }
        for (int j = start; j<start+count; j++) {
            totals[j] = height;
        }
    }
    for (int i=0;i<totals.length; i++){
        System.out.print(totals[i]);
    }
}
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.