Code Golf: Spider webs
Asked Answered
J

15

36

The challenge

The shortest code by character count to output a spider web with rings equal to user's input.

A spider web is started by reconstructing the center ring:

   \_|_/
  _/   \_
   \___/
   / | \

Then adding rings equal to the amount entered by the user. A ring is another level of a "spider circles" made from \ / | and _, and wraps the center circle.

Input is always guaranteed to be a single positive integer.

Test cases

Input
    1
Output
      \__|__/
      /\_|_/\
    _/_/   \_\_
     \ \___/ /
      \/_|_\/
      /  |  \

Input
    4
Output
         \_____|_____/
         /\____|____/\
        / /\___|___/\ \
       / / /\__|__/\ \ \
      / / / /\_|_/\ \ \ \
    _/_/_/_/_/   \_\_\_\_\_
     \ \ \ \ \___/ / / / / 
      \ \ \ \/_|_\/ / / /
       \ \ \/__|__\/ / /
        \ \/___|___\/ /
         \/____|____\/
         /     |     \

Input:
    7
Output:
            \________|________/
            /\_______|_______/\
           / /\______|______/\ \
          / / /\_____|_____/\ \ \
         / / / /\____|____/\ \ \ \
        / / / / /\___|___/\ \ \ \ \
       / / / / / /\__|__/\ \ \ \ \ \
      / / / / / / /\_|_/\ \ \ \ \ \ \
    _/_/_/_/_/_/_/_/   \_\_\_\_\_\_\_\_
     \ \ \ \ \ \ \ \___/ / / / / / / /
      \ \ \ \ \ \ \/_|_\/ / / / / / /
       \ \ \ \ \ \/__|__\/ / / / / /
        \ \ \ \ \/___|___\/ / / / /
         \ \ \ \/____|____\/ / / /
          \ \ \/_____|_____\/ / /
           \ \/______|______\/ /
            \/_______|_______\/
            /        |        \

Code count includes input/output (i.e full program).

Josefajosefina answered 29/10, 2009 at 22:7 Comment(12)
Happy Halloween!Josefajosefina
Awesome. =] Stupid character limit.Brat
Positive and greater than zero? Isn't that a bit superfluous? :)Rotative
@Joren: Brought to you by the Department of Redundancy Department.Corvus
Is it OK to use argv or similar for the input?Headmaster
@Kinopiko: Yes, see older golf questions.Josefajosefina
@Chris - The only thing worse than redundancy jokes are recursion jokes.Montford
@Chris - A man walks into a bar to tell a joke about a man who walks into a bar...Chiastolite
Once upon a time, there were code golf challenges that didn't involve ASCII art! I guess that's just a fairy tale now. ;-)Inconsiderable
Not really, check out mine :)Gristede
@Ken: Number crunching golfs are boring ;)Josefajosefina
@Ken: Check mine out, then: https://mcmap.net/q/236555/-code-golf-four-is-magicAbm
C
18

Golfscript - 124 chars

All whitespace is significant! If you accidently add a newline to the end there will be an extra _ at the end of the output

~):@,{@\:&-:0' ': *& '/':/+*'\\':~'_':
0*.'|':|\/~ +&*n}%
/+@*   ~
+@*n ~+@*


@/ +*n@,{):& *@&-:( ~+*/[
 ](!=&*.|\~/ +(*n}%

Golfscript - 129 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*   '\_'@*n ~+@*


@'/ '*n@,{):& *@&-:( ~+*'/'[
 ](!=&*.'|'\~'/ '(*n}%

Golfscript - 133 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*3 *'\_'@*n' \\'@*3
*@'/ '*n@,{):& *@&-:( ~+*'/''_ '1/(!=&*.'|'\~'/ '(*n}%
Caruthers answered 29/10, 2009 at 22:7 Comment(4)
Only have to get rid of 63 more chars to beat this solution!Octodecillion
Only 47 chars to go now. Look out.Octodecillion
Drat, Perl loses by 37 characters. I tried removing every fourth character from my solution, but that made it stop printing out spider webs.Octodecillion
@mobrule, your solution had better presentation, for what it's worth :)Josefajosefina
O
40

Perl, 164 chars

195 184 171 167 164

print@o=((map{$z=_ x($x=1+$N-$_);$"x$x." /"x$_."\\$z|$z/".'\ 'x$_.$/}0..($N=<>)),
"_/"x++$N."   ".'\_'x$N.$/);
y'/\\'\/',@o||y#_# #,$t++||y#_ # _#,print while$_=pop@o

First statement prints out the top half of the spider web. Second statement uses transliteration operations to create a reflection of the top half.

This next one weighs in closer to 314 chars (of productive code), but is more in the spirit of the season.

           ;               "
         Tr                 Ic
        K|                   |t
       Re                     aT
       ",                     "H
       av                     e
        A:                    -
     )H AL                   LO  W
    ee  N"                   ,"  En
   jo    y_                 Yo    ur
   _      C&&             y";     ##
   &I      ();           $N=      1+
   <>;      $,=  $/;@O  =((     map 
    $" x($   X=$N-$_). ${   f}x$_.$
        B.${U}x$X.$P.${U}x$X.$
    F.${b}x$_,0..$N-1),${g}x$N.(${S}
   x3).${c}x$N);sub I{($F,$B,$U, $P)
  =qw         (/ \\ _ |);;         ${
 S}=        " ";$f=$S.$F;$g=$       U.
 $F     ;$b=$B.$S;$c=$B.${U};}@{    P}=
@{     O};  while($_=pop@{P}  ){     @{
 P}    ||   y:_: :;$spooky++  ||    0|
  0    ||   y@_ @ _@;y:/:8:;   ;    ;
   ;   ;;   y:\\:/:;y:8:\\:;  @O   =
    (  @O    ,$_);}print@O;   q{
       Do     !Discuss:Rel    ig
       io       n,Politi      cs
        ,&                   &T
        heG                 rea
          tP              ump
            ki           n}

Hat tip to http://www.ascii-art.de/ascii/s/spider.txt

I constructed the spider shaped code by hand, but see the Acme::AsciiArtinator module on CPAN for help with automating (or at least semi-automating) the task.

Octodecillion answered 29/10, 2009 at 22:7 Comment(6)
I think you may have too many of those alphabetical letters in thereChiastolite
(Kibbitzing) -2 by changing "\\ " to '\ ' and similarly for "\\_".Headmaster
(More kibbitzing) Can change the first "print" to "warn" for -1. A bit dubious though.Headmaster
Thanks KP. turns out you can also use y''' and get rid of one more `\`Octodecillion
Actually I tried the y''', and got an error message. I didn't notice that it was possible the way you did it.Headmaster
Trick || TreaT, Enjoy Your C&&y, Have A :-) HALLOWeeN, Do !Discuss: Religion, Politics, && The Great Pumpkin am I missing anything else? :DJosefajosefina
C
18

Golfscript - 124 chars

All whitespace is significant! If you accidently add a newline to the end there will be an extra _ at the end of the output

~):@,{@\:&-:0' ': *& '/':/+*'\\':~'_':
0*.'|':|\/~ +&*n}%
/+@*   ~
+@*n ~+@*


@/ +*n@,{):& *@&-:( ~+*/[
 ](!=&*.|\~/ +(*n}%

Golfscript - 129 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*   '\_'@*n ~+@*


@'/ '*n@,{):& *@&-:( ~+*'/'[
 ](!=&*.'|'\~'/ '(*n}%

Golfscript - 133 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*3 *'\_'@*n' \\'@*3
*@'/ '*n@,{):& *@&-:( ~+*'/''_ '1/(!=&*.'|'\~'/ '(*n}%
Caruthers answered 29/10, 2009 at 22:7 Comment(4)
Only have to get rid of 63 more chars to beat this solution!Octodecillion
Only 47 chars to go now. Look out.Octodecillion
Drat, Perl loses by 37 characters. I tried removing every fourth character from my solution, but that made it stop printing out spider webs.Octodecillion
@mobrule, your solution had better presentation, for what it's worth :)Josefajosefina
C
9

Python - 212 chars

n=input()+1;b,f,p,u,s='\/|_ '
a=[s*(n-i)+' /'*i+b+u*(n-i)+p+u*(n-i)+f+'\ '*i+s*(n-i)for
i in range(n)]
print"\n".join(a+['_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n]+[x[::-1]for
x in a[:0:-1]]+[a[0][::-1].replace(u,s)])
Caruthers answered 29/10, 2009 at 22:7 Comment(0)
S
5

Perl: 161 characters

Note that this code includes the starting web in the source. (The doubled backslash at the end is a shame. An earlier version didn't have that.)

$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
for$x(1..<>){
s|(.\S).*([/\\].)|$1$&$2|g;
s|\\(.*)/| \\_$1_/$` /$&\\ |;
s|(\s+)\K/(.*).$| \\$&/$1 /_$2_\\|
}
s|_(?=.*$)| |g;
print

The whitespace within $_ is significant (of course), but none of the rest is. If you have a minor suggestion that improves this, please feel free to just edit my code. For example, Kinopiko has nicely shaved off 6 characters!

Depending on how you count command-line switches, this might be shorter (154 by usual Perl golf rules if I can count correctly):

#!perl -ap
$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
s|(.\S).*([/\\].)|$1$&$2|g,
s|\S(.*).| \\_$1_/$` /$&\\ |,
s|(\s+)\K/(.*).$| \\$&/$1 /_$2_\\|while$F[0]--;
s|_(?=.*$)| |g
Salami answered 29/10, 2009 at 22:7 Comment(3)
Change the second-to-last line from 1while s|_(.*\\\n)| $1|; to s/_(?=(.*)\s+$)/ /g;Headmaster
Make that s/_(?=.*\s+$)/ /g;.Headmaster
+1 I considered trying something like this, but felt it would be too long. wow! well doneChiastolite
C
4

Vb.net, windows console, Infer, Strict, Explicit ON.

Microsoft word is saying 442 characters without space

It might be possible to reduce it more but this is my last update(try #2)

Module z
Sub Main()
    Dim i = CInt(Console.ReadLine), j = i + 1, h = j * 2 + 1, w = h * 2, z = "_", b = " "

    For y = 0 To h
        For x = 0 To w
            Dim l = (x + y Mod 2 + i Mod 2) Mod 2, u = j + y, e = j - y, k = h + e, o = x = h Or x = h - 1
            Console.Write(If(x = h, If(y = j, b, If(y = j + 1, z, "|")), "") & If(x = w, vbLf, If(y = j, If(x Mod 2 = 0 = (x < h), If(o, b, z), If(x < h, "/", "\")), If(x < k And x > u Or (x < u And x > k Or o) And y < h, z, If(x = k Or (x < u And y < j And x > e Or x > u And y > j And x < w + e) And l = 0, "/", If(x = u Or (x > k And y < j And x < h + u Or x < k And y > j And x > y - j - 1) And l = 1, "\", b))))))
        Next
    Next
End Sub
End Module
Cerellia answered 29/10, 2009 at 22:7 Comment(3)
finally went under 700 characters, vb will never win in this kind of game but was fun to doCerellia
after a rewrite, less than 500 characters :-)Cerellia
a c# version would be less characters for sure, about 60 to 75 less charactersCerellia
O
3

Lua, 290

n=...s=string r=s.reverse g=s.gsub a="\\|/"j=(" /"):rep(n+1)..a..("\\ "):rep(n+1) k=j o=k
l=n*4+7 for i=1,n+1 do k=g(k,"^(.- )/(.-)|(.*)\\(.-)$","%1%2_|_%3%4")o=k..o end
o=o..r(o)print((g(g(g(g(r(g(o:sub(1,l),"_"," ")..o:sub(l+1)),j,g(j," ","_")),("."):rep(l),"%1\n"),a,"   "),r(a),"___")))
Objectionable answered 29/10, 2009 at 22:7 Comment(0)
F
3

Ruby1.8, 179

Run with ruby -n

n=$_.to_i+1
u,s,c=%w{_ \  \ \\}
z=(1..n).map{|i|k=n-i
s*i+c*k+'/'+u*i+'|'+u*i+"\\"+'/ '*k+s*i}
y=z.reverse.map{|a|a.reverse}
z[-1].tr!u,s
puts y,'_/'*n+s*3+'\_'*n,c*n+u*3+'/ '*n,z

In the first attempt below it seemed like a good idea to just generate one quadrant (I chose lower left), and then mirror twice to get the whole web. But gnibbler got better results generating both quadrants (of the top half) and then generating rather than patching up the inner area. So I revised mine to initially generate the other lower quadrant also, mirror only once, and also to leave the innermost row out of the mirror, which kind of converges with the other entry.

Ruby, 241

n=$_.to_i+1
m=2*n+1
u,s,b,f=%w{_ \  \\ /}
z=(0..n).map{|i|s*i+(s+b)*(n-i)+(i==0?u:f)+u*i}
q=z.reverse.map{|a|a.tr f+b,b+b+f}
q[n].gsub!' ','_'
q[n][m-1]=s
z=(q+z).map{|a|a+'|'+a.reverse.tr(f+b,b+b+f)}
z[n][m]=z[n+1][m]=s
z[m].gsub!u,s
puts z
Fabianfabianism answered 29/10, 2009 at 22:7 Comment(0)
C
3

Ruby1.9 - 181 chars

n=gets.to_i+1;s=' '
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+?\\+?_*j+'|'+?_*j+?/+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse};d[-1].tr!?_,s
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+?_*3+'/ '*n,d

Ruby1.8 - 185 chars
Some improvements from JRL

n=gets.to_i+1;s=' '
u='_';a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+'\\'+u*j+'|'+u*j+'/'+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse}
d[-1].tr!u,s;puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,d

Ruby - 207 chars
Ruby seems to have some peculiar rules about the "\"

n=eval(gets)+1
b,f,p,u,s='\/|_ '.split""
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+b+u*j+"|"+u*j+f+"\\ "*i+s*j}
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,a[1..-1].reverse.map{
|x|x.reverse},a[0].reverse.tr(u,s)
Caruthers answered 29/10, 2009 at 22:7 Comment(0)
D
3

Python: 240 Characters

Nothing too tricky here; just printing line by line - 298 280 271 266 265 261 260 254 240 characters (ignore the last 2 line breaks)

u,b,f,s,a='_\/ |'
m=input()+1
print'\n'.join([(m-x)*s+x*' /'+b+(m-x)*u+a+(m-x)*u+f+x*'\ 'for x in
  range(0,m)]+['_/'*m+s*3+'\_'*m+'\n'+(s+b)*m+u*3+'/ '*m]+[x*s+(m-x)*
  ' \\'+f+x*u+a+x*u+b+(m-x)*'/ 'for x in range(1,m)] + [s*m+f+s*m+a+s*m+b])
Duet answered 29/10, 2009 at 22:7 Comment(10)
u,b,f,s,a='_\/ |' you don't need to double the backslash hereChiastolite
In Python 3 you would be able to simply say p=print (since in Py3k print is a function instead of a keyword). But input would have to be wrapped in int. Saves 5 chars.Chocolate
m=int(raw_input())+1 since you don't use n anywhereChiastolite
Thank again, gnibbler - I used to use n - forgot that I didn't anymore.Duet
@Stephan - Thanks! Yeah, I figured it was possible in py3k. I had tried it in 2.6 and it didn't work; no doubt keywords take precedence over functions. Why does input need to be wrapped in int? Has input changed behaviour in py3k?Duet
input in P3 works like raw_input. Since this is P2, why do you have paren around a for the print?Chiastolite
@Caruthers - Haha - silly me. Yeah, I'm in 2.6 and am just getting used to the "best-practise" of treating print as a function rather than as a keyword. however, here at code golf, best-practise goes out the window ;-) Thanks! One more character!Duet
Now we wait for the Perl/Ruby hackers to come along and blow us Python users out of the water...Duet
Yes, but by the time the ruby/perl guys tee off the Python guys are at the 19th hole ;)Chiastolite
Actually Python's doing OK against Ruby, I posted a scoreboard as a second answer.Fabianfabianism
H
2

C, 573 chars

Obviously it isn't even in the running w/regard to the character count. The 573 number is just the file size on my windows machine, so that probably counts a few ctrl-M's. On the other hand, maybe 573 is under-counting it, since I incurred the wrath of the compiler by jettisoning all the #include's to save space, warnings be damned!

But hey, this is my first time attempting one of these, and it will undoubtedly be good practice to try to re-express it in something more compact.

#define B puts("");
#define K '\\'+'/'
#define F '_'+' '
#define P(s) putchar(s);
#define I int
c(I s,I f){if(s){P(f)c(s-1,f);P(f)}else P('|')} 
w(I lw,I s,I k,I f){if(s){P(' ')P(k)w(lw,s-1,k,f);P(K-k)P(' ')}else{P(K-k)c(1+lw,f);P(k)}}
h(I g,I s,I k,I f){I i;for(i=-1;i<g;++i)P(' ')w(g,s,k,f);} 
t(I g,I s){if(s)t(g+1,s-1);h(g,s,'/','_');B}
b(I g,I s){h(g,s,'\\',s?'_':' ');B;if(s)b(g+1,s-1);}
m(I s,I k,I f){if(s){P(f)P(k)m(s-1,k,f);P(K-k)P(f)}else{P(F-f)P(F-f)P(F-f)}}
main(I ac,char*av[]){I s;s=atoi(av[1]);t(0,s);m(1+s,'/','_');B;m(1+s,'\\',' ');B;b(0,s);}
Heliogabalus answered 29/10, 2009 at 22:7 Comment(0)
L
1

dc - 262

A "straightforward" solution in dc (OpenBSD). Not a contender, but it is always fun. Line breaks for "readability"

[lcP1-d0<A]sA?sN[lK32sclAxRlNlK-l1scd0!=ARl3PlKl0sclAxRl9PlKlAxRl4PlNlK-
l2scd0!=AAPR]sW95s0124s9[ /]s1[\\ ]s292s347s4lN[dsKlWx1-d0<L]dsLx
[\\_][   ][_/][lN[rdPr1-d0<L]dsLxRRPlNlLxRR]dsBxAP[/ ][_ _][ \\]lBxAP[ \\]s1
[/ ]s247s392s41[dsKlWx1+dlN>L]dsLx32s032s9lNsKlWx

sample output

$ dc web.dc  
3
   \___|___/
   /\__|__/\ 
  / /\_|_/\ \ 
_/_/_/   \_\_\_
 \ \ \_ _/ / / 
  \ \/_|_\/ / 
   \/__|__\/ 
   /       \
Lachesis answered 29/10, 2009 at 22:7 Comment(0)
F
1

Python and Ruby just about even*

I would rather have continued the comment thread above that briefly mentioned Python vs Ruby, but I need formatting to do this. Smashery is certainly classy but doesn't need to worry: it turns out that Python and Ruby are in a pretty close race by one measure. I went back and compared Python to Ruby in the eight code-golf's that I have entered.

    Challenge       Best Python             Best Ruby

    The Wave          161                    99
    PEMDAS          no python entry       (default victory?)
    Seven Segs        160                   175
    Banknotes          83 (beat Perl!)       87  
    Beehive           144                   164
    RPN (no eval)     111 (157)              80 (107)
    Cubes             249                   233
    Webs              212                   181

    Victories           3                     4 (5?)

So the issue definitely isn't settled and got more interesting recently when gnibbler started entering on both sides. :-)


*I only counted fully functional entries.

Fabianfabianism answered 29/10, 2009 at 22:7 Comment(4)
My 249 char for Cubes in Python is fully functional, but there was a comment there that made it look like it was still broken, so Cubes is quite close. I'll have to do a Python entry for PEDMAS!Chiastolite
You should totally graph this data, DigitalRoss. Because of junk [code-golf] questions, may I suggest at least 10 votes for a statistical question?Josefajosefina
Both of you: good points, please feel free to edit my CW stuff, I can think of no one more qualified than you two...Fabianfabianism
At a completely pointless metric, yes, they're even. And both got spanked by Golfscript. :-)Inconsiderable
P
1

Python, 340 - 309 - 269 - 250 characters

Still room for improvement I think.

s=input()+1
f,b="/ ","\\"
r=range(s)
for i in r:w="_"*(s-i);print" "*(s+(i>=1)-i)+(f*i)[:-1]+b+w+"|"+w+"/"+"\ "*i
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+f*s
for i in r[::-1]:u="_ "[i<1]*(s-i);print" "*(s-i+(i>=1))+("\ "*i)[:-1]+"/"+u+"|"+u+b+f*i

-

Python (alternative version), 250 - 246 characters

s=input()+1;r=range(s);c="/","\\";y="/ ","\\ "
def o(i,r):u="_ "[i<1 and r]*(s-i);print" "*(s+(i>=1)-i)+(y[r]*i)[:-1]+c[r<1]+u+"|"+u+c[r]+(y[r<1]*i)[:-1]
for i in r:o(i,0)
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+"/ "*s
for i in r[::-1]:o(i,1)
Prejudice answered 29/10, 2009 at 22:7 Comment(3)
Could just use input() rather than int(raw_input()) - returns an intDuet
Because Python treats strings as iterable, you should be able to simplify your assignment to c: c='_ 'Duet
(i>=1) beats (1,0)[i<1]. Can replace indentation with semicolons. Space after print not required.Woodbury
S
0
(&)=(++) --9
f 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"] --52
f(n+1)=[s&h&u&"|"&u&g]&w(f n)&[s&g&s&"|"&s&h]where[a,b,c,d,e]=" _/\\|";[g,h]=["/","\\"];y=n+2;[u,s]=[r y b,r y a];p f s n x=let(a,b)=span(/=s)x in a&f b;i=dropWhile(==a);w[]=[];w[x]=[s&h&i(p(map(\x->if x==a then b else x))c d x)&g];w(l:e)|n==y*2-1=x%h:z|n>y=x&" "%" \\":z|n==y="_/"%"\\_":z|n<y=r(y-n)a&"\\ "%" /":z where n=length e;z=w e;x=r(n+1-y)a&g;(%)=(&).(&i l) --367
r=replicate --12
main=interact$unlines.f.read --29

Haskell entry weighing in at 469 characters. I'm sure there is a lot of room for improvement.

good luck trying to read it :)

here is a more readable version. Although there have been some changes since this version

spider 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"]
spider n=(s++"\\"++u++"|"++u++"/"):w m(spider(n-1))++[s++"/"++s++"|"++s++"\\"]
    where
        [a,b,c,d,e]=" _/\\|"
        [m,y]=[y*2,n+1]
        x=r y
        [u,s]=[x b,x a]
        t a b=map(\x->if x==a then b else x)
        p f s n x=let(a,b)=span(/=s)x;(c,d)=span(/=n)b in a++f c++d
        i=dropWhile(==a)
        w _[]=[]
        w _[x]=[s++"\\"++i(p(t a b)c d x)++"/"]
        w(a+1)(l:e) |a==m-1=wrapline x l"\\":z
                    |a>y=wrapline(x++" ")l" \\":z
                    |a==y=wrapline"_/"l"\\_":z
                    |a<y=wrapline(r(y-a)' '++"\\ ")l" /":z
            where
                z=w a e
                x=r(a+1-y)' '++"/"
                wrapline b l a=b++i l++a
r=replicate
main=interact$unlines.spider.read
Sapsago answered 29/10, 2009 at 22:7 Comment(0)
G
0

Perl 264 chars

shortened by in-lining the subroutines.

perl -E'$"="";($i=<>)++;@r=map{$p=$i-$_;@d=(" "x$_,(" ","\\")x$p,"/","_"x$_);($d="@d")=~y:\\/:/\\:;@d=reverse@d;$d.="|@d"}1..$i;say for reverse@r;$_=$r[0];y: _|:_  :;s:.(.*)\\.*/(.*).:$1_/   \\_$2:;say;y: _\\/:_ /\\:;say;$r[-1]=~y:_: :;say for grep{y:\\/:/\\:}@r;'

Expanded to improve readability.

perl -E'
  $"="";
  ($i=<>)++;
  @r=map{
    $p=$i-$_;
    @d=(
      " "x$_,
      (" ","\\")x$p,
      "/",
      "_"x$_
    );
    ($d="@d")=~y:\\/:/\\:;
    @d=reverse@d;
    $d.="|@d"
  }1..$i;
  say for reverse@r;
  $_=$r[0];
  y: _|:_  :;
  s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
  say;
  y: _\\/:_ /\\:;
  say;
  $r[-1]=~y:_: :;
  say for grep{y:\\/:/\\:}@r;
'

This is the code before I minimized it:

#! /opt/perl/bin/perl
use 5.10.1;

($i=<>)++;
$"=""; #" # This is to remove the extra spaces for "@d"

sub d(){
  $p=$i-$_;
  " "x$_,(" ","\\")x$p,"/","_"x$_
}

sub D(){
 @d=d;
 ($d="@d")=~y:\\/:/\\:; # swap '\' for '/'
 @d=reverse@d;
 $d.="|@d"
}

@r = map{D}1..$i;

say for reverse@r; # print preceding lines

# this section prints the middle two lines
$_=$r[0];
y: _|:_  :;
s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
say;
y: _\\/:_ /\\:;
say;

$r[-1]=~y:_: :; # remove '_' from last line
say for grep{y:\\/:/\\:}@r; # print following lines
Grewitz answered 29/10, 2009 at 22:7 Comment(1)
Neither of the sub parts are necessary. Both of them are only called in one place, so they can be inlined into the code.Headmaster

© 2022 - 2024 — McMap. All rights reserved.