安定したレンガの壁を構築する


39

レンガの壁は、横に1行n列のレンガが積み重なった長方形です。これは、高さ4と幅8の壁で、右側にレンガのサイズが表示されています。

[______][______]    4 4
[__][____][__][]    2 3 2 1
[][______][____]    1 4 3
[____][______][]    3 4 1

この壁には障害があります。これは、周囲のレンガ内に括弧が付いている、レンガ間の2つの垂直亀裂が並ぶ場所であるためです。

[______][______]    
[__][____)(__][]
[][______)(____]
[____][______][]

ただし、右側のサイズ1のレンガに隣接する亀裂は、列で区切られているため、障害にはなりません。

指定されたサイズのレンガで構築された安定した壁を見つけて表示するコードを記述します。最少バイトが勝ちます。

入力

空白ではないレンガサイズ(正の数)のリストと、少なくとも2の高さ。このリストは、必要に応じて並べ替えることができます。代わりに、各サイズのレンガの数を取り入れることができます。

出力

指定されたすべてのレンガを使用した、必要な高さの安定した長方形の壁の写真。印刷するか、改行付きの文字列として返します。

角括弧で囲まれたアンダースコアを2n文字としてサイズnのブリックを描画します。

1: []
2: [__]
3: [____]
4: [______]
...

入力には、少なくとも1つのソリューションがあることが保証されています。複数ある場合でも、壁を1つだけ描画する必要があります。

時間制限はありません。必要なだけブルートフォースを使用します。アルゴリズムは、理論的にはあらゆるサイズの入力で動作するはずです。

テストケース:

複数のソリューションがあるため、出力が異なる場合があります。

>> [1, 1, 2, 2], 2
[][__]
[__][]

>> [1, 1, 1, 2, 2, 2, 2, 3], 2
[__][____][__]
[][__][][__][]

>> [1, 1, 2, 2, 3, 3, 3, 3], 3
[][__][____]
[__][____][]
[____][____]

>> [1, 2, 3, 4, 5, 6, 7, 8, 9], 5
[][______________]
[__][____________]
[________________]
[____][__________]
[______][________]

>> [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 5
[][__][__]
[__][__][]
[][__][__]
[__][__][]
[][__][__]

なぜn> 1文字幅ではなく2n文字幅のレンガを作ることにしたのですか?
スパー

2
@Sparr 1 x 2文字ブロックはほぼ正方形に見えます。私はそれを要求しようとしましたがn>1、それがテストケースを制限する方法が好きではありませんでした。また、明らかに先例があります。
xnor

n> 1で2nを意味しません。私はn> 1でnを意味します。
スパー

回答:


20

Perl、166 170194

Larry Wallが作成した言語に最適なタスク。

#!perl -pa
$_=(1x($x=2/($y=pop@F)*map{1..$_}@F)."
")x$y;sub
f{my$l=$_;$-|=!@_;for$=(@_){$Z=__
x~-$=;$f=0;s/(11){$=}/[$Z]/&!/\]..{$x}\[/s&&f(grep$=ne$_||$f++,@_);$-or$_=$l}}f@F

ブルートフォースですが、テストケースでは非常に高速です(1秒未満)。使用法:

$ perl ~/wall.pl <<<"1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 5"
[][__][__]
[__][__][]
[][__][__]
[__][__][]
[][__][__]

私をテストしてください


9
ハ、私はラリー・ウォールが人々がそのような言語を使うだろうと思ったことはあるだろうか... :)
crazyhatfish

12

CJam、94 92 82バイト

これは92バイトのバージョンです。82バイトバージョンが続きます。

l~1$,:L,:)m*{1bL=},\e!\m*{~W<{/(\e_}%}%{::+)-!},{{_,,\f<1fb}%2ew{:&,(},!}={{(2*'_*'[\']}/N}/

これにより、ブリックはあらゆる可能な方法に分割され、有効な方法のみが使用されます。今のところかなり強引ですが、私のマシンのJavaインタープリターで約10秒で最後のテストケースを実行します。

説明

コードは5つの部分に分かれています。

1)lengthの配列が与えられた場合L、どのようにそれをH部分に分割できるか。

l~1$,:L,:)m*{1bL=},
l~                     e# Read the input as string and evaluate it.
  `$,:L                e# Copy the array and take its length. Store that in L
       ,:)             e# Get an array of 1 to L
          m*           e# Cartesian power of array 1 to L of size H (height of wall)
            {1bL=},    e# Take only those parts whose sum is L

この後、入力配列をHブリックレイヤーに分割するあらゆる方法があります。

2)入力配列のすべての順列を取得し、さらにすべての順列のすべてのパーティションを取得します

\e!\m*{~W<{/(\e_}%}%
\e!                    e# Put the input array on top of stack and get all its permutations
   \m*                 e# Put the all possible partition array on top and to cartesian
                       e# product of the two permutations. At this point, every
                       e# permutation of the input array is linked up with every
                       e# permutation of splitting L sized array into H parts
      {           }%   e# Run each permutation pair through this
       ~W<             e# Unwrap and remove the last part from the partition permutation
          {     }%     e# For each part of parts permutation array
           /           e# Split the input array permutation into size of that part
            (\         e# Take out the first part and put the rest of the parts on top
              e_       e# Flatten the rest of the parts so that in next loop, they can be
                       e# split into next part length

この後、入力ブリックの可能なすべてのレイアウトがHレイヤーブリックウォールになります。

3)ブリックの長さが同じレイアウトのみを除外する

{::+)-!},
{      },              e# Filter all brick layouts on this condition
 ::+                   e# Add up brick sizes in each layer
    )-!                e# This checks if the array contains all same lengths.

このフィルターの終了後、残りのレイアウトはすべて完全な長方形になります。

4)安定性基準に一致する最初のブリックレイアウトを取り出す

{{_,,\f<1fb}%2ew{:&,(},!}=
{                       }=   e# Choose the first array element that leaves truthy on stack
 {         }%                e# For each brick layer
  _,,                        e# Create an array of 0 to layer length - 1
     \f<                     e# Get all sublists starting at 0 and ending at 0
                             e# through length - 1
        1fb                  e# Get sum of each sub list. This gives us the cumulative
                             e# length of each brick crack except for the last one
           2ew               e# Pair up crack lengths for every adjacent layer
              {    },        e# Filter layer pairs
               :&            e# See if any cumulative crack length is same in any two
                             e# adjacent layers. This means that the layout is unstable
                 ,(          e# make sure that length of union'd crack lengths is greater
                             e# than 1. 1 because 0 will always be there.
                     !       e# If any layer is filtered through this filter,
                             e# it means that the layer is unstable. Thus negation

このステップの後、レイアウトを印刷するだけです

5)レイアウトを印刷する

{{(2*'_*'[\']}/N}/
{               }/           e# For each brick layer
 {           }/              e# For each brick
  (2*'_*                     e# Get the (brick size - 1) * 2 underscores
        '[\']                e# Surround with []
               N             e# Newline after each layer

こちらからオンラインでお試しください


82バイト

l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g{{(2*'_*'[\']}/N}/

これは、92バイトバージョンとほとんど同じですが、ランダム性が少しあります。92バイトバージョンの説明を読んだ場合、82バイトバージョンでは、パート3、4、および5はまったく同じですが、パート1および2からのすべての順列を繰り返すのではなく、このバージョンは単に一度に順列し、パート3と4を使用してテストし、パート3と4のテストが失敗した場合にプロセスを再起動します。

これにより、最初の3つのテストケースの結果が非常にすばやく出力されます。height = 5のテストケースは、まだコンピューターに出力されていません。

違いの説明

l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g
l~:H;                           e# Eval the input and store the height in H
     {   ...   }g               e# A do-while loop to iterate until a solution is found
      e_mr                      e# Flatten the array and shuffle it.
          H({               }%  e# This is the random partition generation loop
                                e# Run the loop height - 1 times to get height parts
             H-X$,+(            e# While generating a random size of this partition, we
                                e# have to make sure that the remaining parts get at least
                                e# 1 brick. Thus, this calculation
                    mr)         e# Get a random size. Make sure its at least 1
                       /(\e_    e# Similar to 92's part 2. Split, pop, swap and flatten

_::+)-                          e# 92's part 3. Copy and see if all elements are same
      X${_,,\f<1fb}%2ew{:&,(},  e# 92's part 4. Copy and see if layers are stable
+,                              e# Both part 3 and 4 return empty array if
                                e# the layout is desirable. join the two arrays and
                                e# take length. If length is 0, stop the do-while

このバージョンのアイデアは、randomra(Get it?)によって与えられました。

これをオンラインで試す


9

パイソン2、680の 670 660バイト

なぜ私がこれらの超長距離の「ゴルフ」を主張するのかわかりませんが...とにかく、ここに行きます。

M,L,R,N=map,len,range,None
exec"J=@:M(''.join,x);B=@:'['+'_'*2*~-x+']';K=@:M(B,x);W=@:J(M(K,x));C=@:set(M(sum,[x[:i]for i in R(L(x))]))-{0};T=@,w:w[x:]+w[:x]\ndef F(i):f=filter(@:i[x-1]&i[x],R(1,L(i)));return f and f[0]".replace('@','lambda x')
def P(e,x,i,w,h):
 for j in[-~_%h for _ in R(i-1,h+i-2)]:
    for s in R(w):
     if not e&C(T(s,x[j])):return j,s
 return N,N
def b(l,h):
 w,d=[[]for _ in R(h)],2*sum(l)/h
 for _ in l[::-1]:q=M(L,W(w));w[[q.index(i)for i in sorted(q)if i+L(B(_))<=d][-1]]+=_,
 g=M(C,w);i=F(g)
 while i:
    e=g[i-1];j,s=P(e,w,i,d,h)
    if j!=N:w[j]=T(s,w[j]);w[i],w[j]=w[j],w[i];g=M(C,w);i=F(g)
    else:b(T(-1,l),h);return
 print'\n'.join(W(w))

これには、ソートされた昇順の出力が必要で、経由で呼び出されb(brick_sizes, height)ます。

テストケース:

>>> tests = [([1, 1, 2, 2], 2),([1, 1, 1, 2, 2, 2, 2, 3], 2), ([1, 1, 2, 2, 3, 3, 3, 3], 3), ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5), ([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 5)]
>>> for t in tests:
...     b(*t); print
... 
[__][]
[][__]

[____][__][__]
[][][__][__][]

[____][____]
[__][__][][]
[____][____]

[________________]
[______________][]
[____________][__]
[__________][____]
[________][______]

[__][__][]
[][__][__]
[__][__][]
[][__][__]
[__][__][]

これが機能する方法は次のとおりです。

  1. 次のレイヤーに移動する前に各レイヤーを塗りつぶして、レンガにレイヤーを割り当てます(最長->最短)。
  2. 隣接するレイヤーが不安定な場合は、機能するものが見つかるまでレイヤーを交換してレンガを移動してみてください。
  3. 何も機能しない場合は、最も長いレンガをサイズリストの先頭に移動して再帰します。

1
あなたはおそらくcontinue終わり近くからドロップすることができます。また、return(N,N)括弧を必要としません。
-PurkkaKoodari

良いコール-それcontinueは以前のバージョンからの遺物でした。
サーパーシバル

1
実行できません。無関係なブラケットがWありT、余分な引数が渡されます。
crazyhatfish

おっと、ありがとう!一定。
サーパーシバル

5

Haskell、262バイト

import Data.List
c=concat
n=init.scanl1(+)
1%l=[[[l]]]
n%l=[map(h:)(c$(n-1)%t)|(h,t)<-map(`splitAt`l)[1..length l]]
v[x]=1<2
v(x:y:z)=sum x==sum y&&n x==n x\\n y&&v(y:z)
l#n=unlines$map(>>=(\b->'[':replicate(2*b-2)'_'++"]"))$head$filter v$c.(n%)=<<permutations l

使用例:

*Main> putStr $  [1, 2, 3, 4, 5, 6, 7, 8, 9] # 5
[______][________]
[__________][____]
[____________][__]
[][______________]
[________________]

*Main> putStr $ [1, 1, 2, 2, 3, 3, 3, 3] # 3
[____][____]
[__][__][][]
[____][____]

仕組み:メイン関数#はリストl(ブリックのリスト)と数字h(高さ)を取り、すべての可能な順列ですべての順列lhサブリストに分割します(関数を介して%、たとえば2%[1,2,3,4]-> [ [[1],[2,3]] , [[1,2],[3]] , [[1,2,3],[]] ])。2つの連続した要素の合計が同じ(つまり、レンガで同じ長さ)で、小計のリストに共通の要素がない(つまり、亀裂が並んでいない、関数v)ものを保持します。適合する最初のリストを取り、一連のレンガを作成します。


4

パイソン2、528417393、381

非常に長い、ブルートフォースソリューション。それは動作しますが、それはそれについてです、最後のテストケースの結果を得る前に宇宙は終わるかもしれません。

exec u"from itertools import*;m=map;g=@w,n:([[w]],[[w[:i]]+s#i?range(1,len(w))#s?g(w[i:],n-1)])[n>1];r=@x:set(m(sum,[x[:i]#i?range(1,len(x))]));f=@w:1-all(m(@(x,y):not x&y,zip(m(r,w[:-1]),m(r,w[1:]))));a=@s,h:['\\n'.join([''.join(['[%s]'%(' '*(s-1)*2)#s?r])#r?o])#p?permutations(s)#o?g(p,h)if len(set([sum(r)#r?o]))<2 and~-f(o)][0]".translate({64:u"lambda ",35:u" for ",63:u" in "})

aは主な機能です。

>> a([1, 1, 2, 2], 2)
'[][  ]\n[  ][]'

インポートを呼び出しに変更し、呼び出しからfrom itertools import*削除することにより、4バイトを節約できます。また、最後のsを...に変更すると、13バイト節約できます。itertools.permutationsifif all(x==w[0] for x in w)and~-f(o):return
-PurkkaKoodari

また、f最初の反復で常に戻るとは限りませんか?それは奇妙に見えます。それはバグか巨大なゴルフの機会のどちらかです。
-PurkkaKoodari

あなたはまた、割り当てているなど、オペレータを囲む引用符/括弧/括弧の前または後に-あなたを除去することができる余分なスペースのトンを持っているt=0に二回r()。その関数をmap(sum,[x[:i] for i in range(len(x))])ワンライナーとして作成できます(必要に応じて、ラムダ処理に適しています)。isdisjointとset inを使用f()すると、大幅に削減されます(f()エラーが検出されたかどうかに関係なく、現在は1回のテストだけで戻ります)。個人的にはf()return not all(map(isdisjoint,map(set,map(r,w[:-1])),map(set,map(r,w[1:]))))またはそれに似たように書き換えます。
サーパーシバル

@ Pietu1998そうそう、スペースを見逃した。ヒントを提供してくれた人たちに感謝します。これらのものを見つけることができるのは驚きです。
crazyhatfish

あまりにも悪い笑って、私は「宇宙は結果を取得する前に終了しても良い」のコードのようなものを嫌うが、これはです最短バイトcotest xDで行うには他に何
Abr001am

3

JavaScriptの(ES6)222 232 265 279 319

まだゴルフされています。これはすべての解決策を見つけ、最後に見つかったものだけを出力し、それは非常に高速です。

Firefoxでスニペットを実行してテストする

f=(n,h,b=[],s=0)=>
  (R=(z,l,p,k,t)=>
    z?b.map((v,a)=>
      v&&k.indexOf(v=t+a)<0&v<=s&&(
        --b[a],h=l+`[${'__'.repeat(a-1)}]`,
        v-s?R(z,h,[...p,v],k,v):R(z-1,h+'\n',[],p,0),
        ++b[a]
      ))
    :n=l
  )(h,'',[],[],0,n.map(n=>(b[n]=-~b[n],s+=n)),s/=h)&&n

// Test suite


out=x=>OUT.innerHTML=OUT.innerHTML+x+'\n'

;[ 
 [[1, 1, 2, 2], 2], [[1, 1, 1, 2, 2, 2, 2, 3], 2], [[1, 1, 2, 2, 3, 3, 3, 3], 3]
,[[1, 2, 3, 4, 5, 6, 7, 8, 9], 5], [[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 5]]
.forEach(([a,b])=>out(a+' '+b+'\n'+f(a,b)))
<pre id=OUT></pre>

Ungolfedそして説明

function f(n, h) {
  var b=[], s=0, result // in golfed version will re-use n for result variable
  n.forEach(function (n) {
    b[n] = -~b[n] // group equal input numbers in buckets
    s+=n          // calc sum of input numbers
  });
  // example of buckets: input 1,1,4,1,5,4 -> b[1]=3,b[4]=2,b[5]=1
  s /= h // total sum / height => sum expected for each brick layer

  // recursive scan function 
  function R(z, // layer count, from h downto 1
             l, // output so far
             p, // current layer partial sums array, mark intervals between bricks
             k, // prev layer parial sums, checked to avoid faulds
             t  // current partial sum 
             ) 
  {
    if (z > 0) 
    { // still building
      b.forEach( function (v,a) { // a:number from input list, v: repeat count 
        var w, m   // locals (in golfed version, reuse other variables avoid defining locals)
        w = t + a; // increased running total for current layer
        if (v != 0  // repeat count still > 0 
           && k.indexOf(w) < 0 // running total not found on list in prev layer (no fault)
           && w <= s) // current layer length not exceeded
        {
           --b[a]; // decrease repeat count, number used one more time
           m = l+"["+ '__'.repeat(a-1) + "]"; // new output with a brick added
           // l is not changed, it will be used again in this loop
           if (w == s) 
           {   // layer complete, go to next (if any)
               // recurse, new layer, add newline to output, p goes in k, and t start at 0 again
               R(z-1, m+'\n', [], p, 0); 
           }
           else
           {   // layer still to complete
               // recurse, same layer, m goes in l, add current sum to array p
               R(z, m, [...p,w], k, w);
           }
           ++b[a]; // restore value of repeat count for current loop
        }
      })
    }   
    else
    { // z == 0, all layers Ok, solution found, save in result and go on to next
      result = l;
    }
  }

  R(h,'',[],[],0);
  return result; // this is the last solution found
}

2

Python 2、グリッド方式(290文字)

x,h=input()
from itertools import *
w = sum(x)*2/h
for p in permutations(x):
 bricks = ''.join('[' + '_'*(2*n-2) + ']' for n in p)
 cols = map(''.join,zip(*zip(*[iter(bricks)]*w)))
 if all(c=='[' for c in cols[0]) and all(c==']' for c in cols[-1]) and not any(']]' in col or '[[' in col for col in cols[1:-1]):
  print('\n'.join(map(''.join,zip(*cols))))
  print()

ここでの方法は、グリッドを転置し、列内[[または]]列内の任意の場所を探すことです。また、壁の左右のレンガがすべて揃っていることをテストします。ここでのかわいいことは、文字列のすべての要素が同じであることをテストすることです。'[[[[[['.strip('[')==''


上記のミニバージョン:

x,h=input()
from itertools import*
w=sum(x)*2/h
z=zip
j=''.join
for p in permutations(x):
 C=map(j,z(*z(*[iter(j('['+'_'*(2*n-2)+']'for n in p))]*w)))
 if C[0].strip('[')==''and C[-1].strip(']')==''and not any(']]'in c or '[['in c for c in C[1:-1]):
  print('\n'.join(map(j,z(*C))))
  break

これは、おそらくマトリックス操作言語でより簡単に行うことができます。

...または正規表現の乱用。これにより、「ブロックが端で整列する」状態と「クラックなし」状態を組み合わせることができます。

壁の幅がw = 6だったとします。部分文字列「[..... [」および「] .....]」の位置は、正確に{0、w-1、w、2w-1,2w、3w-1のセットでなければなりません。 ..}。これらのポイントに存在しないということは、次のようなレンガの「ラインラップ」を意味します。

       v
[][__][_
___][__]
       ^

これらの点に存在しないということは、壁に不安定な「ひび割れ」があることを意味します。

     vv
[][__][]
[    ][]
     ^^

したがって、問題を減らして等価性を設定します。ここで、問題のセットは正規表現一致のインデックスです。

# assume input is x and height is h

from itertools import *
import re
w=sum(x)*2/h

STACKED_BRACKET_RE = r'(?=\[.{%i}\[|\].{%i}\])'%(w-1,w-1)  # ]....] or [....[
STRING_CHUNK_RE = '.{%i}'%w  # chunk a string with a regex!
bracketGoal = set().union(*[(x*w,x*w+w-1) for x in range(h-1)])  # expected match locations

for p in permutations(x):
 string = ''.join('['+'_'*(2*n-2)+']'for n in p)
 bracketPositions = {m.start() for m in re.finditer(STACKED_BRACKET_RE,string)}
 print(string, bracketPositions, bracketGoal, STACKED_BRACKET_RE) #debug
 if bracketPositions==bracketGoal:
  break

print('\n'.join(re.findall(STRING_CHUNK_RE,string)))

Python、regexpメソッド(304文字):

from itertools import*
import re
x,h=input()
w=sum(x)*2/h
for p in permutations(x):
 s=''.join('['+'_'*(2*n-2)+']'for n in p)
 if {m.start()for m in re.finditer(r'(?=\[.{%i}\[|\].{%i}\])'%(w-1,w-1),s)}==set().union(*[(x*w,x*w+w-1) for x in range(h-1)]):
  break

print('\n'.join(re.findall('.{%i}'%w,s)))

障害を直接確認するために壁の画像を直接使用する興味深いアイデア。のように入力を取得するための行が必要x,h=input()です。
xnor

0

Matlab(359)

function p(V),L=perms(V);x=sum(V);D=find(rem(x./(1:x),1)==0);for z= 2:numel(D-1)for y=1:numel(L),x=L(y,:);i=D(z);b=x;l=numel(x);for j=1:l,for k=j-1:-1:2,m=sum(x(1:k));if mod(m,i),if mod(m,i)<mod(sum(x(1:k-1)),i)||sum(x(1:j))-m==i,b=0;,end,end,end,end,if b,for j=1:l,fprintf('[%.*s]%c',(b(j)-2)+b(j),ones(9)*'_',(mod(sum(x(1:j)),i)<1)*10);end,return,end;end,end

入力

整数のベクトル、例:p([1 1 2 2 3])

出力

壁の例のスキーム:

[____]

[__][]

[][__]
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.