ASCII蛇行曲線を描く


19

この質問の目的のために、蛇行曲線は、左から右への一般的な方向に従う曲線ですが、左に90度でn + 1回転を繰り返し、次にn + 1が右に回転します(n> 0の場合)。

実際、蛇行自体にnセグメントがあります。

ターンはで示され+ます。

蛇行の幅(2つの間の距離+)は、水平線で3(---)、垂直線で1(|)です。

以下は、1〜5のサイズnの蛇行曲線の単一セグメントです。

                                                           +-------------------+
                                                           |                   |
                                       +---------------+   |   +-----------+   |
                                       |               |   |   |           |   |
                       +-----------+   |   +-------+   |   |   |   +---+   |   |
                       |           |   |   |       |   |   |   |   |   |   |   |
           +-------+   |   +---+   |   |   +---+   |   |   |   +---+   |   |   |
           |       |   |   |   |   |   |       |   |   |   |           |   |   |
   +---+   +---+   |   +---+   |   |   +-------+   |   |   +-----------+   |   |
   |   | 1     |   | 2         |   | 3             |   | 4                 |   | 5
---+   +-------+   +-----------+   +---------------+   +-------------------+   +   

チャレンジ:

2つの正の数nとを与えて、サイズの蛇行曲線のセグメントをm描きmますn。完全なプログラムまたは関数を作成できます。

入力:

n > 0曲線のサイズ

m > 0描画するセグメントの数

出力:

蛇行曲線のASCII表現。

例:

n = 3
m = 2
   +-----------+   +-----------+   
   |           |   |           |
   |   +---+   |   |   +---+   |
   |   |   |   |   |   |   |   |
   +---+   |   |   +---+   |   |
           |   |           |   |  
-----------+   +-----------+   +

n = 2
m = 5
   +-------+   +-------+   +-------+   +-------+   +-------+   
   |       |   |       |   |       |   |       |   |       |   
   +---+   |   +---+   |   +---+   |   +---+   |   +---+   |   
       |   |       |   |       |   |       |   |       |   |   
-------+   +-------+   +-------+   +-------+   +-------+   +

n = 4
m = 4
   +---------------+   +---------------+   +---------------+   +---------------+
   |               |   |               |   |               |   |               |
   |   +-------+   |   |   +-------+   |   |   +-------+   |   |   +-------+   |
   |   |       |   |   |   |       |   |   |   |       |   |   |   |       |   |
   |   +---+   |   |   |   +---+   |   |   |   +---+   |   |   |   +---+   |   |
   |       |   |   |   |       |   |   |   |       |   |   |   |       |   |   |
   +-------+   |   |   +-------+   |   |   +-------+   |   |   +-------+   |   |
               |   |               |   |               |   |               |   | 
---------------+   +---------------+   +---------------+   +---------------+   +

受賞基準:

これはであるため、各言語のバイト単位の最短コードが優先されます。時間がある場合は、コードを説明してください。


1
将来の課題に対する提案:最初の図(蛇行が増加する図)をプロットし、グラフィカルな出力を可能にします
ルイスメンドー

3
n左に曲がっていませんか?
-LiefdeWen

1
@LuisMendoはい、1-5の画像を作成したとき、その中に良い課題があることに気付きました-正の整数のリストLが与えられ、サイズL(i)のセグメントで曲がりくねった曲線を作成します
Galen Ivanov

@LiefdeWenカウントを開始する場所によって異なります。私はそれがだと思うn+1...特に、単一のセグメントの間に、例を見ると
ガレン・イワノフ

回答:


6

52 34 33バイト

Nθ↶FNF⊗⊕θ«+⊖⊗×⊕﹪κ²∨↔⁻θ∧κ⊖κ¹¿›κθ↷↶

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

Nθ

蛇行のサイズを入力します。

図面が右から始まり左に動くので、上方向に旋回します。

FN

必要な数の蛇行をループします。

F⊗⊕θ«

蛇行のセグメントをループします。

+

を印刷し+ます。

∨↔⁻θ∧κ⊖κ¹

kリスト th番目の要素を計算しますnnn1n23211123n

⊖⊗×⊕﹪κ²...

水平線の長さは2倍なので、長さを2倍または4倍にしますが、印刷前に結果をデクリメントして、印刷されたものを考慮します+

¿›κθ↷↶

次のセグメントのために適切にピボットします。



3

Pythonの3371の 354 346 328 298 290バイト

import sys
v=sys.argv
s=int(v[1])
n=range
r="   |"
t="   +---"
h="-"*4
e=" "*4
def C(l):print(l*int(v[2]))
for i in n(-(-s//2)):q=s-i+~i;R=r*i;C(R+t+h*q+"+"+R);C(R+r+e*q+R+r)
for i in n(s//2):q=s//2-i;R=r*~-q;w=2*i+s%2;C(R+t+h*~-w+"+"+r*(q+((i>0)|s%2)));C(R+e*-~w+R+2*r)
C(h*~-s+"---+   +")

-20B ceilingcatに感謝

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

ゴルフ前:


import sys

import math

def draw_curve(curve_size, curve_count, out=sys.stdout):
    for i in range(math.ceil(curve_size / 2)):
        for j in range(curve_count):
            out.write("   |" * i)
            out.write("   +---")
            out.write("----" * (curve_size - 2 * i - 1))
            out.write("+")
            out.write("   |" * i)

        out.write("\n")

        for j in range(curve_count):
            out.write("   |" * (i + 1))
            out.write("    " * (curve_size - 2 * i - 1))
            out.write("   |" * (i + 1))

        out.write("\n")

    for i in range(curve_size // 2):
        for j in range(curve_count):
            out.write("   |" * (curve_size // 2 - i - 1))
            out.write("   +---")
            out.write("----" * (2 * i - (0 if curve_size % 2 else 1)))
            out.write("+")
            out.write("   |" * (curve_size // 2 - i + (1 if i or curve_size % 2 else 0)))

        out.write("\n")

        for j in range(curve_count):
            out.write("   |" * (curve_size // 2 - i - 1))
            out.write("    " * (2 * i + (2 if curve_size % 2 else 1)))
            out.write("   |" * (curve_size // 2 - i + 1))

        out.write("\n")

    for j in range(curve_count):
        out.write("----" * (curve_size - 1))
        out.write("---+   +")

if __name__ == "__main__":
    draw_curve(int(sys.argv[1]), int(sys.argv[2]))

2

C(gcc)559 540 523 511 494 484 476 468 468 456 447 443バイト

#define S memset
l,o,p,q,r;g(c,d,n)char*d;{q=~c;for(p=n*2;q%2*p;bcopy(n*2-p--?"|   |":"+---+",d-q*l*p,5));p=n-1;c--%2?S(S(d-~l-r*!q,45,r++)-2*l,45,r=n*4-1),d[r*=q-1]='|',d[r-l]=d[r+l]=43,p&&g(c%4,memcpy(q?d+p*4+l:d-l-n*4,q?"+   |":"|   +",5),p):p&&g(c%4,d,p,d[-q]=45,d[q*=l]=43,*(d-=q*(n*2*l-l-2)-2)=32);}f(n,m){char b[(o=n-~n)*(l=n*4+5)];g(0,strcpy(S(S(b,32,l*o),45,l)+l-6,"+   +"),n);for(o*=m;b[o/m*l-1]=0,o--;o%m||puts(""))printf(b+o/m*l);}

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

ゴルフがやや少ない

#define S memset
l,o,p,q,r;
g(c,d,n)char*d;{
 q=~c;
 for(p=n*2;q%2*p;bcopy(n*2-p--?"|   |":"+---+",d-q*l*p,5));
 p=n-1;
 c--%2?
  S(S(d-~l-r*!q,45,r++)-2*l,45,r=n*4-1),
  d[r*=q-1]='|',
  d[r-l]=d[r+l]=43,
  p&&
   g(c%4,memcpy(q?d+p*4+l:d-l-n*4,q?"+   |":"|   +",5),p)
 :
  p&&
   g(c%4,d,p,d[-q]=45,d[q*=l]=43,*(d-=q*(n*2*l-l-2)-2)=32);
}
f(n,m){
 char b[(o=n-~n)*(l=n*4+5)];
 g(0,strcpy(S(S(b,32,l*o),45,l)+l-6,"+   +"),n);
 for(o*=m;b[o/m*l-1]=0,o--;o%m||puts(""))
  printf(b+o/m*l);
}

1

ダッシュ -POSIXシェルスクリプト、528バイト

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

ゴルフ:

Y=0;p(){ eval A${1}_${2}='$3';};for Z in `seq $2`;do case $1 in 1)L='r3 u1 r3 d1';;2)L='r7 u1 l3 u1 r7 d3';;3)L='r11 u3 l3 d1 l3 u3 r11 d5';;4)L='r15 u5 l7 d1 r3 d1 l7 u5 r15 d7';;5)L='r19 u7 l11 d3 r3 u1 r3 d3 l11 u7 r19 d9'
esac;for A in $L;do d(){ C=-;case "$1" in r*)X=$((X+1));;l*)X=$((X-1));;u*)Y=$((Y+1));C=\|;;d*)Y=$((Y-1));C=\|;;esac;p $X $Y $2 $C;};for I in `seq ${A#*[a-z]}`;do d $A;done;d $A +;done;done;for Y in `seq 20 -1 0`;do for X in `seq 0 99`;do eval F="\"\$A${X}_${Y}\"";L=${L}${F:- };done;echo "$L";L=;done

なし:

#!/bin/sh

# helper function for emulating an array, the language does not know it
p(){ eval A${1}_${2}='$3';}
Y=0

for Z in `seq $2`;do

 # define the possible patterns:
 # list="direction+count direction+count ..."
 case $1 in
  1)L='r3 u1 r3 d1';;
  2)L='r7 u1 l3 u1 r7 d3';;
  3)L='r11 u3 l3 d1 l3 u3 r11 d5';;
  4)L='r15 u5 l7 d1 r3 d1 l7 u5 r15 d7';;
  5)L='r19 u7 l11 d3 r3 u1 r3 d3 l11 u7 r19 d9'
 esac

 for A in $L;do
  # helper function for going into needed direction
  # and plot char into array
  d(){
   C=-
   case "$1" in
    r*)X=$((X+1));;
    l*)X=$((X-1));;
    u*)Y=$((Y+1));C=\|;;
    d*)Y=$((Y-1));C=\|;;
   esac
   p $X $Y $2 $C
  }

  # write char as long as needed into array,
  # append in the same direction as last element the '+'
  for I in `seq ${A#*[a-z]}`;do
   d $A
  done
   d $A +
  done
 done

# echo the array linewise
for Y in `seq 20 -1 0`;do
 for X in `seq 0 99`;do
  eval F="\"\$A${X}_${Y}\"";L=${L}${F:- }
 done
 echo "$L";L=
done

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