木々の森が見えない


29

樹木を描画するプログラムまたは関数を作成して、森林を構築します。

木はピラミッドを積み重ねるように描かれます。最初(一番上)の行が含まれている1木を、次の行の下には含まれ2(合計3)、次が含まれている3(合計6)など。行全体を完成させるのに十分な木がない場合は、左に記入し、右側のスポットを空のままにします。さらに、下位レベルのツリーは、その配置のために上位レベルのツリーとわずかに重なります。

これはサイズの森です 1

  /\
 //\\
///\\\
  ||
  ||

これはサイズの森です 2

      /\
     //\\
  /\///\\\
 //\\ ||
///\\\||
  ||
  ||

これはサイズの森です 3

      /\
     //\\
  /\///\\\/\
 //\\ || //\\
///\\\||///\\\
  ||      ||
  ||      ||

これはサイズの森です 4

          /\
         //\\
      /\///\\\/\
     //\\ || //\\
  /\///\\\||///\\\
 //\\ ||      ||
///\\\||      ||
  ||
  ||

これはサイズの森です5(5番目のツリーの上部が最初のツリーの幹を覆っていることに注意してください)

          /\
         //\\
      /\///\\\/\
     //\\ || //\\
  /\///\\\/\///\\\
 //\\ || //\\ ||
///\\\||///\\\||
  ||      ||
  ||      ||

(いくつかスキップ)
これはサイズの森です8(パターンを拡張)

              /\
             //\\
          /\///\\\/\
         //\\ || //\\
      /\///\\\/\///\\\/\
     //\\ || //\\ || //\\
  /\///\\\/\///\\\||///\\\
 //\\ || //\\ ||      ||
///\\\||///\\\||      ||
  ||      ||
  ||      ||

等々。

入力

単一の正の整数、任意の便利な形式でn > 0

出力

上記のルールに従う、フォレストのASCIIアート表現。ツリーがすべて適切に整列している場合、先頭または末尾の改行またはその他の空白はオプションです。

ルール

  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

木が描画される順序に関するパターンが何なのかわかりません。つまり、与えられたn、木の位置は何ですか?
ルイスメンドー

@LuisMendo私が理解するように、それらは読み順で満たされています。したがって、各行は順番にいっぱいになり、行全体に十分なツリーがない場合、残りはその行の可能な限り左に配置されます。
XNOR

@LuisMendo xnorにはそれがあります。それをより明確にするために言い直せれば、チャットで私にpingを送ってください。
AdmBorkBork

@xnorありがとう、それは今私には完全に明確です
ルイスメンドー

@Adm実際には、それはチャレンジのすぐそこに書かれていました。どうやら私は読むことができません:
ルイスメンドー

回答:


5

Haskell 310バイト

w i=putStr$unlines$reverse$b i 0 0[][]
b 0 _ _ w r=e w r
b c l 0 w r=b c(l+1)l(e w r)[]
b c l p w r=b(c-1)l(p-1)w(n(++)["  ||    ","  ||    ","///\\\\\\  "," //\\\\   ","  /\\    "]r)
e w r=t++n(n d)(map(\t->"    "++t)w)c where(t,c)=splitAt 2 r
n f(a:c)(b:d)=f a b:n f c d
n _ a[]=a
n _ _ a=a
d d ' '=d
d _ d=d

w 5たとえば、で呼び出します。

ここに非圧縮コード:

-- TreeTree
-- by Gerhard
-- 12 February 2017

module TreeTree (wood,test) where

type Tree = [String]

-- Test cases
test = do
 wood 0
 wood 1
 wood 2
 wood 3
 wood 4
 wood 5

-- build wood
wood :: Int -> IO ()
wood i = printTree $ buildWood i 0 0 [] []

-- Prints the trees
printTree :: Tree -> IO ()
printTree = putStr . unlines . reverse

-- build wood
buildWood :: Int -> Int -> Int -> Tree -> Tree -> Tree
buildWood 0 _ _ w r = concatTree w r 
buildWood c l 0 w r = buildWood c (l+1) l (concatTree w r) []
buildWood c l p w r = buildWood (c-1) l (p-1) w (addTree r)

-- indent definition
space :: String
space = "    "

-- tree definition
tree :: Tree
tree = reverse [
 "  /\\    ",
 " //\\\\   ",
 "///\\\\\\  ",
 "  ||    ",
 "  ||    "]

-- Add a Tree on the left side
addTree :: Tree -> Tree
addTree = match (++) tree

-- add tree row at the bottom of the wood
concatTree :: Tree -> Tree -> Tree
concatTree w r = trunk ++ matched
 where
  wood = grow w
  (trunk, crown) = splitAt 2 r 
  matched = matchTree wood crown

-- elnarge forrest on the left side to match next tree line
grow :: Tree -> Tree
grow = map (\t -> space ++ t)

-- match
match :: (a -> a -> a) -> [a] -> [a] -> [a]
match f (a:az) (b:bz) = f a b : match f az bz
match _ a [] = a
match _ _ a  = a

-- match trees
matchTree :: Tree -> Tree -> Tree
matchTree = match matchLine

-- match lines
matchLine :: String -> String -> String
matchLine = match matchChar

-- match chars
matchChar :: Char -> Char -> Char
matchChar c ' ' = c
matchChar _ c   = c

-- End

PPCGへようこそ!
AdmBorkBork

4

JavaScript(ES6)、357 297 276バイト

f=
n=>{a=`  /\\`;d=`///\\\\\\`;b=d+`/\\`;c=` //\\\\ ||`;d+=`||`;e=`
`;r=`repeat`;s=``;for(i=1;n>i;n-=i++)s=(s+a+b[r](i-1)+e+c[r](i)).replace(/^/gm,`    `)+e;return(s+a+b[r](n-1)+d[r](i-=n)+e+c[r](n)+(s=`      ||`[r](i))+e+d[r](n)+s+(s=e+`  ||    `[r](n))+s).replace(/\|.$/gm,``)}
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

編集:@KritixiLithosのおかげで21バイトを保存しました。


最初の場合はrepeat、あなたが変更することができますblah.repeat(val)blah[w="repeat"](val)で、その後、あなたはそれ以降の出現箇所変更することができますrepeatだけに[w](val)バイト保存する代わりに
KritixiのLithos

@KritixiLithos最初repeatforループはループ内にあり、のためn=1に実行されないため、私はそれを行うことができませんが、21バイトを保存することができました。
ニール

4

C ++(Windows)、330 312 308 304 303バイト

#import<cstdio>
#import<windows.h>
#define P(x,y,s)SetConsoleCursorPosition(GetStdHandle(-11),{X+x,Y+y});puts(s);
int X,Y,R,r,c;t(){P(2,-2,"/\\")P(1,-1,"//\\\\")P(0,0,"///\\\\\\")P(2,1,"||")P(2,2,"||")}f(int n){for(c=R=r=1;c<n;c+=++R);for(;r;r++)for(c=0;++c<r+1;){X=(R-r-2)*4+c*8;Y=r*2;t();r=--n?r:-1;}}

で呼び出す:

int main()
{
    f(8);
}

0

C(Windows)、297 295 294バイト

#import<windows.h>
#define P(x,y,s)C.X=X+x;C.Y=Y+y;SetConsoleCursorPosition(GetStdHandle(-11),C);puts(s);
COORD C;X,Y,R,r,c;t(){P(2,-2,"/\\")P(1,-1,"//\\\\")P(0,0,"///\\\\\\")P(2,1,"||")P(2,2,"||")}f(n){for(c=R=r=1;c<n;c+=++R);for(;r;r++)for(c=0;++c<r+1;){X=(R-r-2)*4+c*8;Y=r*2;t(r=--n?r:-1);}}

私のC ++の答えに似ていますが、Cの方がやや短いのでこれを投稿しました。


@DLosc It's C. #importは(非推奨の)GCC拡張機能です。しかし、ゴルフに適しています。
Steadybox

面白いね。今、私はそのためのヒントがあることがわかります。あなたの答えでそれを言及するかもしれません。
DLosc

おそらく@DLoscが、私はそれが非常に広く省略するようないくつかの他のGCC(ただしGCCに限定されない)の拡張に伴い、ゴルフで使用されると思う<stdio.h>と、自動的にされるグローバル変数と仮定するとint、リターンや機能をint
Steadybox

0

Javascript 418 377バイト

39バイトのゴルフを手伝ってくれた@Kritixi Lithosに感謝

x=>{s='';for(t=0;++t<x;x-=t);q='//\\\\';z="///\\\\\\";h="/\\";t--;for(i=0;i<t;i++){a=4*(t-i)+1;s+=" "[w="repeat"](a+1)+h+(z+h)[w](i)+`
`+" "[w](a)+q+(" || "+q)[w](i)+`
`}c=t-x+1>0?t-x+1:0;return x?s+"  "+(h+z)[w](--x)+h+(c?(z+"||")[w](c-1)+z:'')+`
 `+q+(" || "+q)[w](x)+" ||     "[w](c)+`
`+(z+"||")[w](x)+z+(c?"||"+"      ||"[w](c-1):'')+`
`+("  ||    "[w](x+1)+`
`)[w](2):''}

オンラインで試す


2
最初の場合はrepeat、あなたが変更することができますblah.repeat(val)blah[w="repeat"](val)、その後、あなたはそれ以降の出現箇所変更することができますrepeatだけに[w](val)バイト保存する代わりに
KritixiのLithos
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.