11
\$\begingroup\$

The task is a rather simple quine challenge with a twist, you must output your source code in the shape of a pyramid. The shape of a pyramid is defined below:

       1
      234
     56789
    ABCDEFG
   HIJKLMNOP
  QRSTUVWXYZa
 bcdefghijklmn
......etc......

The main limitation of this challenge is that your quine must contain exactly enough bytes as to not obstruct the pattern of the pyramid. For instance, the following program lengths would work:

1-byter: 1st layer of the pyramid (not allowed by definition of a quine).
4-byter: 1st and 2nd layers of the pyramid.
9-byter: 1st, 2nd and 3rd layers of the pyramid.
etc...

So, if your program was:

QWERTY

It would not be valid, because it would arrange like:

  Q
 WER
TY

However, if your program was QWERTYUIO, it would be fine:

  Q
 WER
TYUIO

Rules

  • Standard loopholes are obviously disallowed, no reading your own source.
  • The pyramid must be centered, trailing characters are allowed, but not required.
    • Also, any character may be used to center the pyramid, doesn't have to be (char)32.
  • Your quine must be able to be shaped into a pyramid.
    • It must retain the original ordering of your sourcecode.
    • Your source-code may NOT contain the character being used to format the pyramid.
    • E.G. if your sourcecode contains a space, you'll need another char for the format.
  • You may use comments in your quine to "pad" to the correct size.
    • Obviously, these must be output as part of the quine.
  • If the program contains newlines/tabs, they aren't part of the quine and should be omitted in the output.
  • The shape is counted in characters, not bytes; if the shape is malformed you're not doing it right.

The lowest possible score here should be 4.

\$\endgroup\$
  • \$\begingroup\$ Does the original quine need to be in the shape of a pyramid, or does just the output need to be? \$\endgroup\$ – KrystosTheOverlord Feb 15 at 16:46
  • \$\begingroup\$ @KrystosTheOverlord the output, the original doesn't matter. If the original has tabs or newlines you should omit these in the output as well in order to retain the shape of the output. \$\endgroup\$ – Magic Octopus Urn Feb 15 at 17:02
  • \$\begingroup\$ Is it valid if the output has a bunch of trailing spaces/newlines? \$\endgroup\$ – Emigna Feb 15 at 18:04
  • \$\begingroup\$ @Emigna should it be? I don't see a problem with it unless others do at this point. Also, define "a bunch of", isn't the consensus usually "a single trailing newline is acceptable"? \$\endgroup\$ – Magic Octopus Urn Feb 15 at 19:11
  • 1
    \$\begingroup\$ You should make a challenge where you need to have the quine in the format of a pyramid :). \$\endgroup\$ – KrystosTheOverlord Feb 15 at 23:00
4
\$\begingroup\$

05AB1E, 36 bytes

"34çs«DJā·<£õK.cJ?"34çs«DJā·<£õK.cJ?

Try it online!

If trailing characters are allowed also means at the end of the output, 0"D34çýā·<£.c"D34çýā·<£.c is a layer shorter at 25 bytes.

\$\endgroup\$
  • \$\begingroup\$ 34ç is the basis of all 05AB1E quines, isn't it haha? Also, not sure how I feel about the random amount of trailing newlines... I'd prefer someone else to make that call (what's the norm, 1 trailing/preceeding is allowed?) that seems super on-the-edge. \$\endgroup\$ – Magic Octopus Urn Feb 15 at 19:13
  • \$\begingroup\$ @MagicOctopusUrn: Yeah, it's kinda questionable. I assume my shorter version isn't OK, which is why I didn't use it as the primary program, but I felt I should ask since it would save me a whole layer. \$\endgroup\$ – Emigna Feb 15 at 19:33
  • \$\begingroup\$ Dang, you beat me to it. And instead of ā·< I used 9ÅÉ (in the 25 byter, didn't thought about the trailing newline..) \$\endgroup\$ – Kevin Cruijssen Feb 16 at 11:02
  • \$\begingroup\$ @MagicOctopusUrn Btw, all other answers have a trailing newline, so all would be invalid except for this 36-byter.. \$\endgroup\$ – Kevin Cruijssen Feb 16 at 11:07
4
\$\begingroup\$

Java 11, 324 256 227 bytes

v->{var s="v->{vars=%c%s%1$c;for(inti=0;;)System.out.printf(%1$c%%%1$c+(15+i)+%1$cs%%n%1$c,s.format(s,34,s).substring(i*i,++i*i));}///";for(int i=0;;)System.out.printf("%"+(15+i)+"s%n",s.format(s,34,s).substring(i*i,++i*i));}//

-29 bytes thanks to @JoKing.

Outputs with leading spaces to make the triangle. (Note that the whitespace between var s and int i are tabs, not spaces.)

Try it online.

Explanation:

-part:

  • var s contains the unformatted source code String
  • %s is used to put this String into itself with s.format(...)
  • %c, %1$c and 34 are used to format the double-quotes (")
  • %% is used to format the %
  • s.format(s,34,s) puts it all together

Challenge part:

for(int i=0;;)         // Loop `i` indefinitely upwards from 0
  System.out.printf(   //  Print with format:
    "%"+(15+i)+"s      //   Add leading spaces to make the line length size 15+`i`
                 %n",  //   And include a trailing newline
    s.format(s,34,s).substring(
                       //   And append a substring of the source code-String
       i*i,            //    From index `i` squared
       ++i*i));}       //    To index `i+1` squared

Which stops with an java.lang.StringIndexOutOfBoundsException: begin 225, end 256, length 226 error for the String#substring(int,int) method, the iteration after it has printed the result (which is fine according to the meta).

\$\endgroup\$
  • \$\begingroup\$ it doesn't save you any anything really, but you don't need to s=s.format part when you can have the format in the loop instead \$\endgroup\$ – Jo King Feb 16 at 11:47
  • \$\begingroup\$ @JoKing Ah of course. Thanks. Unfortunately I have to decrease the base program without comments to at least 196 (142) to save bytes (or only golf it to 225 (152) and find a workaround to have an odd number of bytes somehow). It's currently 228 without the comments. \$\endgroup\$ – Kevin Cruijssen Feb 16 at 15:08
  • 1
    \$\begingroup\$ Tried a different approach and ended up only two bytes off and only because of it being an odd number... The workaround for odd lengths is a %% in the string and only one % in the actual code, but this means the comments are mandatory \$\endgroup\$ – Jo King Feb 19 at 4:42
  • 1
    \$\begingroup\$ @JoKing Nice approach with the spaces replaced with tabs so we can have leading spaces (and omit the tabs from the output due to the challenge rules). I've been able to rework your code with /// in the string so the string is long enough for it to go to the expected iteration to print everything, before stopping with the StringIndexOutOfBoundsException for the .substring. And with only two trailing // at the end of the actual program, since it only prints two trailing // as well. :) \$\endgroup\$ – Kevin Cruijssen Feb 19 at 13:40
  • \$\begingroup\$ Oh wow, I didn't expect a Java answer! Nice one!!! \$\endgroup\$ – Magic Octopus Urn Feb 19 at 14:27
4
\$\begingroup\$

Python 2, 81 bytes

s='n=0;\nprint(8-n)*chr(32)+("s=%r;exec(s*9)"%s)[n*n:][:n-~n];n+=1;#JK';exec(s*9)

Try it online!

An eval quine that uses spaces as the filler character.

\$\endgroup\$
  • \$\begingroup\$ without error? \$\endgroup\$ – ASCII-only Feb 19 at 8:08
  • \$\begingroup\$ and with spaces as padding \$\endgroup\$ – ASCII-only Feb 19 at 8:31
  • \$\begingroup\$ or... well... clearly with that much padding you could put a signature (i.e. a more interesting string thatn aaaaaaaaa) in it :P \$\endgroup\$ – ASCII-only Feb 19 at 8:35
  • \$\begingroup\$ @ASCII-only Good point, see updated answer ;) \$\endgroup\$ – Jo King Feb 19 at 9:21
3
\$\begingroup\$

Perl 6, 67 bytes

<say("<$_>~~.EVAL".substr($!++²,$!*2-1).indent(8-$!))xx⁸>~~.EVAL

Try it online!

I used a couple of unicode characters in order to squeeze out that extra layer. Outputs using spaces:

       <
      say
     ("<$_
    >~~.EVA
   L".substr
  ($!++²,$!*2
 -1).indent(8-
$!))xx⁸>~~.EVAL

Explanation:

<say("<$_>~~.EVAL"                                  )   >~~.EVAL   # Normal quine
                  .substr($!++²,$!*2-1)              xx⁸  # Split into layered substrings
                                       .indent(8-$!)      # And indent each one
\$\endgroup\$
3
\$\begingroup\$

Python 2, 169 bytes

Uses 0 for formatting the pyramid.

s='s=%r;k=s%%s;print"\\n".join(`1-1`*(12-n)+k[n*n:][:n-~n]for n in range(13))#######################';k=s%s;print"\n".join(`1-1`*(12-n)+k[n*n:][:n-~n]for n in range(13))

Try it online!


Python 2, 147 bytes

This uses the rule If the program contains newlines/tabs, they aren't part of the quine and should be omitted in the output.

s='s=%r;k=s%%s;print"\\n".join(`1-1`*(12-n)+k[n*n:][:n-~n]forninrange(13))##';k=s%s;print"\n".join(`1-1`*(12-n)+k[n*n:][:n-~n]for	n	in	range(13))##

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Gol><>, 36 bytes

":P}r6&56F:M}F0ss|&:P&Roao{|;Nooooo!

Try it online!

an even younger version, 36 bytes

":P}r6&56F:M}R` &:P&Fo|ao{|;Noooooo!

I feel so close to getting it one line shorter, grrrr....

Try it online!

even younger version, 36 bytes

"r2ss6&56F:M}R` &:P&Fo|ao{|;what????

This one's code is smaller, but it still comes out to the same amount sadly, the comment takes up the remainder of the space.

Try it online!

slightly younger version, 36 bytes

"r2ss156F:M}F` o|:PP}Fo|ao{{|;Wowza!

Wowza! Heh, I just used that to fill some space, but the program works, to golf down, I used a few prepushed values rather than using variables!

Try it online!

Older version, 42 bytes

"r2ss0V_~6:&F&:M&F` o|_PPV_Fo|ao|;empty...

This has a trailing newline, and has more chars than i would like...

I'm going to be golfing this severely...

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Clean, 256 bytes

module QQ;import StdEnv,Text;q=dec'#';$n#k=(s<+q<+s<+q)%(n^2,n^2+n*2)=lpad k(16+n)(dec q);Start=join{toChar 10}(map$[0..15]);s="module QQ;import StdEnv,Text;q=dec'#';$n#k=(s<+q<+s<+q)%(n^2,n^2+n*2)=lpad k(16+n)(dec q);Start=join{toChar 10}(map$[0..15]);s="

Try it online!

Pretty much the standard quine, conveniently also a template quine, with the formatting function added.

\$\endgroup\$
  • 1
    \$\begingroup\$ Even though it's code-golf, I love these longer answers. Holy cow that's impressive. \$\endgroup\$ – Magic Octopus Urn Feb 19 at 14:26
2
\$\begingroup\$

R, 169 144 bytes

s='`!`=intToUtf8;cat(sprintf("%*s",11+(i=1:12),substring(paste0("s=",q<-!39,s,q,";eval(parse(t=s))"),(i-1)^2+1,i^2)),sep=!010)';eval(parse(t=s))

Try it online!

           s
          ='`
         !`=in
        tToUtf8
       ;cat(spri
      ntf("%*s",1
     1+(i=1:12),su
    bstring(paste0(
   "s=",q<-!39,s,q,"
  ;eval(parse(t=s))")
 ,(i-1)^2+1,i^2)),sep=
!010)';eval(parse(t=s))

Managed to shorten it by moving to a string that’s parsed rather than an expression that’s departed. Had to use a redefinition of a unary operator to get it under the 144 though.

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 225 bytes

var s="var s={0}{1}{0};for(int i=0;;)WriteLine(string.Format(s,(char)34,s).Substring(i*i++,2*i-1).PadLeft(i+14,(char)2));//";for(int i=0;;)WriteLine(string.Format(s,(char)34,s).Substring(i*i++,2*i-1).PadLeft(i+14,(char)2));//

Uses a STX char as padding. Did not realize Kevin Cruijssen already submitted an exact copy in java before posting until I was done, but I decided to post this anyways.

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.