フェンスを作ってください!


15

チャレンジ

これは簡単な挑戦です。与えられた2つの正の整数 whの幅のASCIIフェンスを作成wしての高さh。フェンスは、次のルールを使用して構築する必要があります。

  • +文字はポストを表します。
  • -文字は、フェンスの幅を表すために使用されます。
  • |フェンスの高さを表すために使用されます。
  • 後は、正確に 3 -文字が出力されている、+文字がなければなりませんその後出力されます。四隅を除いて、aを出力する他の時間は+無効になります。このルールに従うには、左からまたは右から開始することができます(例を参照)が、一貫性が必要です。
  • 丁度 2つの|文字は、出力された+文字がなければなりませんその後出力すること。四隅を除いて、aを出力する他の時間は+無効になります。このルールは、上または下から開始できます(例を参照)が、一貫している必要があります。
  • 各フェンスには正確に4つのコーナーがあり、各コーナーはで表されます+

つまり-、3 文字ごとにを出力する必要があります+。また、2 |文字ごとにを出力する必要があります+

フェンスは常に長方形であり、両方whが決してより大きい100またはより小さいと仮定でき1ます。末尾および/または前の空白は許可されます。

例/テストケース

w = 1
h = 1

+-+ 
| |
+-+


w = 3
h = 2

+---+
|   |
|   |
+---+


w = 5
h = 7

+---+--+ or +--+---+
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
+---+--+    +--+---+

w = 10
h = 5

+---+---+---+-+  or +-+---+---+---+
|             |     |             |
|             |     +             +
+             +     |             |
|             |     |             |
|             |     +             +
+             +     |             |
|             |     |             |
+---+---+---+-+     +-+---+---+---+


w = 4
h = 4

+---+-+ or +-+---+
|     |    |     |
|     |    |     |
+     +    +     +
|     |    |     |
|     |    |     |
+---+-+    +-+---+

ルール



3
ふたりが+触れないかもしれないと理解する権利はありますか?
-xnor

@xnorはい、それは正しいです。
クリスチャンディーン

3
ところで、素晴らしい最初の挑戦。
-xnor

1
@LeakyNunあなたの権利。これは、ルールを作成するときに心に留めていなかったケースです。なぜ+-+-+-+-+-+無効なのかを示すルールを追加しました。混乱させて申し訳ありません。
クリスチャンディーン

回答:


9

C、131バイト

#define L for(i=0,j=w;j;)putchar(i++%4?--j,45:43);puts("+")
f(w,h,i,j,c){L;for(j=1;h;printf("%c%*c\n",c,i,c))c=j++%3?--h,124:43;L;}

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

説明:

// The first and the last line are always similar, so let's use a macro
// to avoid code duplication.
#define L

// Let's initialize 'i' and 'j'. 'i' will be used to keep track of which
// character ('+' or '-') we should print, whereas 'j' starts from the
// input width and the loop terminates when 'j' reaches zero.
for(i=0,j=w;j;)

// We post-increment 'i' and take a modulo 4 of its previous value.
// If i%4 == 0, we print '+' (ASCII 43), otherwise we decrement 'j'
// and print '-' (ASCII 45).
putchar(i++%4?--j,45:43);

// After the loop we print one more '+' and newline.
puts("+")

// The function declaration which takes the two input parameters, and
// also declares three integer variables. These three variables could
// also be declared as global variables with the same bytecount.
f(w,h,i,j,c)

// The start of the function body. We use the macro 'L' to print the 
// first line along with a newline.
{L;

// This loop prints all the lines between the first and the last. 'j'
// keeps track of when we should output a '+' instead of a '|'. 'h',
// which is the input parameter for height, serves as a terminator
// for the loop as it reaches zero.
for(j=1;h;<stuff missing from here>)

// We post-increment 'j' and check if its previous value is divisible
// by three, and if it isn't, we decrement 'h' and assign 124 (ASCII
// value for '|') to 'c'. Otherwise we assign '+' (ASCII 43) to 'c'.
c=j++%3?--h,124:43;

// The following is inside the 'increment' part of the 'for' loop.
// We print the character corresponding to the value of 'c', then
// the same character again, but padded with i-1  spaces before it 
// ('i' hasn't been touched since the first loop, so it still stores
// the length of the first line), then a newline.
printf("%c%*c\n",c,i,c)

// Lastly we print the first line again using the same macro 'L'.
L;}

5

Pythonの3140の 137 128 119 106 105バイト

def f(w,h):a=~-w//3-~w;b=("+---"*w)[:a]+'+';print(b,*[i+' '*~-a+i for i in"||+"*h][:h+~-h//2],b,sep='\n')

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


2
長いですが、問題は修正されました。
GarethPW

1
最後の部分in[w+1+(w-1)//3]]最後の部分の間のスペースを削除すると、バイトを節約できます。
クリスチャンディーン

1
PPCGへようこそ!スペースも削除でき'\n') forます。また、あなたは変更することができます(w-1)~-wどの単項演算子は、バイナリのものよりも高い優先順位を持っているので、あなたは括弧を削除することができます。(h-1)-> ~-hおよび(a-1)-> についても同じです~-aオンラインで試してください
-128

1
また、すべての出力が印刷def f(w,h)されるため、と同じ長さlambda w,hですが、コードをさらにゴルフに役立つ場合は複数行を使用できます
-musicman523

1
a=~-w//3-~w;1バイトを保存するには
フェリペナルディバティスタ

4

Mathematica、165バイト

v=Column;d[w_,y_,m_,n_]:=Table[If[Mod[i,y]==0&&i!=w,m,n],{i,w}];(a="+"<>d[#,3,"-+","-"]<>"+";b=v@d[#2,2,"|\n+","|"];v[{a,Row[{b,""<>Table[" ",#+Floor[#/3]],b}],a}])&

4

ピップ、38バイト

37バイトのコード、-nフラグの場合は+1 。

Ph:'-Xa<>3JW'+PsX#h-2WR:'|Xb<>2J'+^xh

コマンドライン引数として幅と高さを取ります。 オンラインでお試しください!

説明

                         a,b are cmdline args; s is space; x is empty string (implicit)
Ph:'-Xa<>3JW'+
   '-Xa                  String of hyphens of length a
       <>3               grouped into substrings of (maximum) length 3
          JW'+           Join on +, also wrapping the result in +
 h:                      Assign that string to h (mnemonic: "header")
P                        and print it (with newline)

PsX#h-2WR:'|Xb<>2J'+^xh
          '|Xb           String of pipes of length b
              <>2        grouped into substrings of (maximum) length 2
                 J'+     joined on +
                    ^x   and split on empty string (i.e. converted to list of chars)
 sX#h-2                  String of len(h)-2 spaces
       WR:               Wrap the spaces with the list of chars
                         Note 1: WR operates itemwise on lists, so the result is a list,
                          each item of which consists of the spaces wrapped in an item
                          from the list of chars
                         Note 2: the : compute-and-assign meta-operator is here abused
                          to give WR: lower precedence than J and ^ and avoid parentheses
P                        Print that list, newline-separated (-n flag)
                      h  Autoprint the header a second time as the footer

4

木炭、47 45 40バイト

F⁴«¿﹪ι³FIη↓⁺×+¬﹪κ²|FIθ⁺×+¬﹪κ³-P+¿⁼ι¹J⁰¦⁰

説明:各側の-s / |sを順番に描画し+、必要に応じてsを挿入してからで終了することにより機能し+ます。上側と右側を描いた後、最初に戻って逆の順序で描画し、効果的に左側と下側を描画します。回転対称が許可されているかどうかはわかりませんが、許可されている場合は27 25バイトの場合:

F⁴«FI⎇﹪ι²ηθ⁺×+¬﹪κ⁻³﹪ι²-⟲T

上部を描画し、左に回転し、右側に描画し、再び回転し、下部と左側を逆に描画することを繰り返すことにより、上記のアイデアを極限まで引き出します。


1
@LeakyNun前回、Pythを破ったのは、ダイヤモンドを2倍にすることでした。
ニール

4

JavaScript(ES6)、133 132バイト

w=>h=>(a="-".repeat(w).replace(/--?-?/g,"+$&")+`+`)+(`
|`.padEnd(a.length)+`|`).repeat(h).replace(/(\|( +)\|\n)\1/g,`$&+$2+
`)+`
`+a

カリー化構文の入力を受け取りますf(width)(height)

テストスニペット

f=
w=>h=>(a="-".repeat(w).replace(/--?-?/g,"+$&")+`+`)+(`
|`.padEnd(a.length)+`|`).repeat(h).replace(/(\|( +)\|\n)\1/g,`$&+$2+
`)+`
`+a
O.innerHTML=f(W.value=5)(H.value=10)
<div oninput="O.innerHTML=f(+W.value)(+H.value)">
W <input id=W type=number min=1> H <input id=H type=number min=1>
</div>
<pre id=O>



2

Java(OpenJDK 8)178 177バイト

w->h->{int i=0;String t="",m,r;for(;i<w;)t+=i++%3<1?"+-":"-";r=t+="+\n";m=t.format("|%"+(t.length()-3)+"s|\n","");for(i=0;i<h;)r+=i++%2<1&i>1?m.replace("|","+")+m:m;return r+t;}

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

@KevinCruijssenのおかげで-1バイト


パラメーターをカリー化することでバイトを保存できます:w->h-> ここで試してください。
ケビンCruijssen

S:うん、私は常に...それは私が自然見つける何かではありませんカリー化を忘れる
オリヴィエ・グレゴワール

1

47 45 37バイト

A…+---÷⁺²×⁴N³αA…+||÷⁺¹×³N²βPα↓βα+↖↑⮌β

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

  • 文字列作成の記号で遊んだ後に保存された2バイト。
  • フェンスの長さを計算するはるかに簡単な方法を考え出したニールのおかげで、8バイトが節約されました。

異なるアプローチニールさん@:最初の私は文字列を作成αし、β使用して、水平および垂直の境界線で文字を含むRange所定の長さに到達するまでの文字列の繰り返しを作成する作業を。次に、適切な順序で印刷します。

  • カーソルを動かさずにαを印刷します。
  • βを下向きに印刷します。
  • αを印刷します。
  • 「+」を印刷します。
  • カーソルを左上に移動します。
  • βを上向きに印刷します。

詳細バージョンへのリンク。


1
について思い出させてくれてありがとうRange、それは私の2番目のアプローチで3バイトを節約します!
ニール

@Neilそれはうれしいです。私はあなたを追い抜いたばかりで、信じられないからです。:
チャーリー

1
さらに良いことに、私はあなたの表現を最適化して、8バイトを節約しました:A…+---÷⁺²×⁴N³αA…+||÷⁺¹×³N²βPα↓βα+↖↑⮌β
ニール

@ニールすごい。そのような最適化。非常に炭。
チャーリー

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