how to use code to create a Pyramid

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#16 Post by dbenham » 16 Mar 2017 16:31

Ach, Of course. :roll:

I was trying to think of a way to get rid of the quotes, and failed to see the obvious.

I'm tapped out of ideas for further "improvement"


Dave Benham

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to use code to create a Pyramid

#17 Post by penpen » 16 Mar 2017 16:48

I tweaked Aacinis first version; "pyramid.bat":

Code: Select all

@cmd /q /e:on /v:on /cset "rows=17"^&set "line=A"^
&(for /l %%a in (2,1,%rows%) do set "line= !line!")^
&for /l %%a in (1,1,%rows%) do echo(!line!^&set "line=!line: A=AAA!"


I also tweaked dbenhams version improved by Aacini (73 Bytes):

Code: Select all

@set s=                 A
:A
@echo %s%&set s=%s: A=AAA%&goto%s:~0,2%A
:AA


penpen

Aacini
Expert
Posts: 1919
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#18 Post by Aacini » 16 Mar 2017 17:26

penpen wrote:I also tweaked dbenhams version improved by Aacini (73 Bytes):

Code: Select all

@set s=                 A
:A
@echo %s%&set s=%s: A=AAA%&goto%s:~0,2%A
:AA


penpen


I like this code very much! :mrgreen:

But you may omit the zero in "goto%s:~0,2%A", so the result may be 1 byte shorter...

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#19 Post by dbenham » 16 Mar 2017 18:13

Sweeet :!: :D

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#20 Post by dbenham » 16 Mar 2017 18:57

I just realized penpen took 3 steps forward, and 2 steps back.

Aacini already pointed out the unnecessary 0.

The old way of manipulating the string with a substring and appending is actually shorter than the find/replace.

Down to 70 bytes :D

Code: Select all

@set s=                 A
:A
@echo %s%&set s=%s:~1%AA&goto%s:~,2%A
:AA


Dave Benham

Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: how to use code to create a Pyramid

#21 Post by Squashman » 16 Mar 2017 19:12

Getting so much enjoyment out of this thread.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to use code to create a Pyramid

#22 Post by penpen » 16 Mar 2017 20:04

I could remove the second label (68 bytes):

Code: Select all

@set s=                 A
:A
@ 2>nul echo%s%&&set s=%s:~1%AA&&goto A


penpen

Aacini
Expert
Posts: 1919
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#23 Post by Aacini » 16 Mar 2017 20:08

Animated_Diamond.bat:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "seed=10"
set "spaces=            "
cls
echo/

for /L %%# in () do (
echo/
set /A "i=9-seed, seed=(seed+1)%%10"

for /L %%i in (0,1,9) do (
   set /A j=i, i+=1
   set "digits="
   for /L %%j in (0,1,%%i) do (
      set /A "j=(j+1)%%10"
      set "digits=!digits! !j!"
   )
   set "spaces=!spaces:~1!"
   echo !spaces!!digits!
)

set /A i+=1
set "spaces=!spaces! "

for /L %%i in (1,1,9) do (
   set /A j=i, i+=2
   set "digits="
   for /L %%j in (%%i,1,9) do (
      set /A "j=(j+1)%%10"
      set "digits=!digits! !j!"
   )
   echo !spaces!!digits!
   set "spaces=!spaces! "
)

echo/
timeout /T 1 > CON
)

Antonio

Aacini
Expert
Posts: 1919
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#24 Post by Aacini » 16 Mar 2017 20:18

penpen wrote:I could remove the second label (68 bytes):

Code: Select all

@set s=                 A
:A
@ 2>nul echo%s%&&set s=%s:~1%AA&&goto A


penpen


One byte shorter:

Code: Select all

@set s=                 A
:A
@echo%s% 2>nul&&set s=%s:~1%AA&&goto A

Antonio

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to use code to create a Pyramid

#25 Post by penpen » 16 Mar 2017 20:31

@Aacini: Nice Diamond :D

I'm unsure if such a "pyramid.bat" (53 byte) is allowed (probably not):

Code: Select all

@set s=%~1A
:A
@echo%s% 2>nul&&set s=%s:~1%AA&&goto A

But it were more dynamic than before:

Code: Select all

Z:\>pyramid.bat

Z:\>pyramid.bat " "
A

Z:\>pyramid.bat "  "
 A
AAA

Z:\>pyramid.bat "   "
  A
 AAA
AAAAA

Z:\>pyramid.bat "    "
   A
  AAA
 AAAAA
AAAAAAA

Z:\>pyramid.bat "     "
    A
   AAA
  AAAAA
 AAAAAAA
AAAAAAAAA
Beside this i am currently out of ideas.


penpen

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: how to use code to create a Pyramid

#26 Post by misol101 » 16 Mar 2017 20:35

For some reason penpen's and Aacini's last two versions give the wrong output on my Win7 machine. If you don't mind a garbage output file (called A of course), this is also 68 bytes:

Code: Select all

@set s=                 A
:A
@echo %s%&set s=%s:~1%AA&goto%s:~,2%A 2>A

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to use code to create a Pyramid

#27 Post by penpen » 16 Mar 2017 20:51

misol101 wrote:For some reason penpen's and Aacini's last two versions give the wrong output on my Win7 machine.
I have no access to win 7, and winxp output seems to be ok.
There is only an additional space at the end (which should be allowed);
if not just use the "2>nul" in front of the echo (adding a byte).
Could you post this output, if it is something different?


penpen

PS: :shock: 4 am... gn8 to all.

Aacini
Expert
Posts: 1919
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#28 Post by Aacini » 16 Mar 2017 20:57

Another byte shorter:

Code: Select all

@set s=                 A
:A
@set s=%s:~1%AA&echo%s% 2>nul&&goto A

Antonio

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: how to use code to create a Pyramid

#29 Post by misol101 » 16 Mar 2017 21:08

penpen: Oops, sorry, my bad, too late at night I guess. I had two versions in the same file, essentially looking like:

Code: Select all

@set s=                 A
:A
@ 2>nul echo%s%&&set s=%s:~1%AA&&goto A
@goto :eof

@set s=                 A
:A
@echo %s%&set s=%s:~1%AA&goto%s:~,2%A 2>A


which jumps to the wrong :A and produces:

Code: Select all

                A
                AAA
              AAAAA
              AAAAAAA
            AAAAAAAAA
            AAAAAAAAAAA
          AAAAAAAAAAAAA
          AAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


However when I had:

Code: Select all

@set s=                 A
:A
@echo %s%&set s=%s:~1%AA&goto%s:~,2%A 2>A
@goto :eof

@set s=                 A
:A
@echo %s%&set s=%s:~1%AA&goto%s:~,2%A 2>A


it produced the right output (it's too late for me to think of why) :lol:


Anyway, this still goes, and it's down to 62 :) :

Code: Select all

@set s=                 A
:A
@set s=%s:~1%AA&echo%s% 2>A&&goto A

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to use code to create a Pyramid

#30 Post by penpen » 17 Mar 2017 05:54

I like the new one - now we could remove the extra space after the A's in the output (but still 66/64 bytes):

Code: Select all

@set s=                 A
:A
@set s=%s:~1%AA&2>nul echo%s%&&goto A

Code: Select all

@set s=                 A
:A
@set s=%s:~1%AA&2>A echo%s%&&goto A


penpen

Post Reply