32
\$\begingroup\$

Write an \$n\$-bytes program \$AB\$ such that:

  • it outputs an integer \$x,\ 0\lt x\lt n\$;
  • both its \$x\$-byte prefix \$A\$ and \$(n-x)\$-byte suffix \$B\$, when run as programs, output \$AB\$.

\$A, B\$ and \$AB\$ should:

  • run in the same language
  • not take any input
  • use the same output method

This is , so the shortest code in bytes wins (i.e., your score is \$ n \$).

Program example (in a dummy 'lizard language')

With \$x = 2\$ bytes, the whole program is lizard the first-part program is li, and the second-part program is zard, then the following behaviour would be expected:

 source | output |
--------+--------+
lizard  | 2      |
li      | lizard |
zard    | lizard |
\$\endgroup\$
10
  • 12
    \$\begingroup\$ Perhaps more clearer phrasing would be to write two programs A and B that output AB (A and B concatenated together). When that output is run as a program, it outputs the length of A. \$\endgroup\$
    – user
    Commented Jul 26, 2021 at 21:25
  • 2
    \$\begingroup\$ @user thanks, you make me realize what's ambiguous in "first n bytes run as programs" as they can be scrambled... Now is it clear? I'd like to stick to this phrasing just because it better evokes the image of a lizard that regenerates its tail. \$\endgroup\$
    – Domenico
    Commented Jul 26, 2021 at 22:23
  • 1
    \$\begingroup\$ @golden_bat I never would have guessed the suggested imagery was about a lizard regenerating its tail if it wasn't for your comment above. \$\endgroup\$ Commented Jul 29, 2021 at 7:18
  • 1
    \$\begingroup\$ @golden_bat no, you should leave it as "output", but just say that all 3 programs should use the same output method. \$\endgroup\$
    – pxeger
    Commented Jul 29, 2021 at 10:09
  • 1
    \$\begingroup\$ I think the ambiguity was actually with the pronoun "it" in the original phrasing. Now it has been rephrased so the issue is resolved. The only reason I have not deleted the comment is because I still think the second suggestion holds. It's much better esp. for newbies who might not know the ropes well if the scoring criteria are in the challenge without having to click a tag. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 29, 2021 at 12:12

8 Answers 8

15
\$\begingroup\$

Python 2, 123 bytes


u="x='\\nu=%r;exec u'%u;print x+`'input(56)'`+x";exec u'input(56)'
u="x='\\nu=%r;exec u'%u;print x+`'input(56)'`+x";exec u

Try it online!

outputs 56 and crash.

Both


u="x='\\nu=%r;exec u'%u;print x+`'input(56)'`+x";exec u

and

'input(56)'
u="x='\\nu=%r;exec u'%u;print x+`'input(56)'`+x";exec u

return the original code.


Here is another version without any crashing of the program:

Python 2, 141 bytes


u="x='\\nu=%r;exec u'%u;print x+`'print 62;exit()'`+x";exec u'print 62;exit()'
u="x='\\nu=%r;exec u'%u;print x+`'print 62;exit()'`+x";exec u

Try it online!


Thanks to @ovs who helped me to improve my solution.

\$\endgroup\$
1
  • \$\begingroup\$ 146 bytes by explicitly exiting when printing the number. A few more bytes can be saved if you exit by error which should be fine as well: Try it online! \$\endgroup\$
    – ovs
    Commented Jul 27, 2021 at 8:54
10
\$\begingroup\$

Gol><>, 52 bytes

"/+:zQash|2ss}a' 0'rlKH
 0"/+:zQash|2ss}a' 0'rlKH
 0

Try it online! Outputs 26.

The two halves (identical, 26 bytes):

"/+:zQash|2ss}a' 0'rlKH
 0

Try it online! Outputs the original program.

The program path for half and full programs is identical. The only difference is that one more 0 is pushed while going through the 2nd column. If the sum of the top two values is 0, it prints 26 and halts (ash). Otherwise:

2ss}   Push 34 and place it at the bottom of the stack
a' 0'  Push a newline and ' 0' on the stack
rlK    Reverse the entire stack and push a copy of the entire stack
H      Print each char from top to bottom and halt
\$\endgroup\$
10
\$\begingroup\$

Scala 3 -language:postfixOps, 940 748 bytes

This is a chonky lizard.

@main def m:Unit=
 val s="""@main def m:Unit=
 val s=Q%sQ;var x=0;class main extends annotation.Annotation{def dval=print(420);def d=printf(s.replace(""+81.toChar,"\""*3),s,s)}
 return
  new main()dval

 x=0
 @main def n={val s=Q%sQ;printf(s.replace(""+81.toChar,"\""*3),s,s)}""";var x=0;class main extends annotation.Annotation{def dval=print(420);def d=printf(s.replace(""+81.toChar,"\""*3),s,s)}
 return
  new main()dval

 x=0
 @main def n={val s="""@main def m:Unit=
 val s=Q%sQ;var x=0;class main extends annotation.Annotation{def dval=print(420);def d=printf(s.replace(""+81.toChar,"\""*3),s,s)}
 return
  new main()dval

 x=0
 @main def n={val s=Q%sQ;printf(s.replace(""+81.toChar,"\""*3),s,s)}""";printf(s.replace(""+81.toChar,"\""*3),s,s)}

x is 420.

Try it online!

Try the first x bytes online!

Try the last (n-x) bytes online!

I'm sure I've done something silly here and can make it smaller, but it's really hard editing quines and seeing what's wrong, so I'll try golfing it more later.

First x bytes, prettified (... contains the entire program):

@main def m: Unit =
  val s = """..."""
  var x = 0
  class main extends annotation.Annotation {
    def dval = print(420)
    def d = printf(s.replace("" + 81.toChar, "\"" * 3), s, s)
  }
  return new main().d

Last (n-x) bytes, prettified (... contains the entire program):

val x = 0
@main def n = {
  val s = """..."""
  printf(s.replace("" + 81.toChar, "\"" * 3), s, s)
}
\$\endgroup\$
2
  • 9
    \$\begingroup\$ That's a dragon, not a lizard! \$\endgroup\$
    – pxeger
    Commented Jul 28, 2021 at 17:57
  • \$\begingroup\$ @pxeger It may seem like a terrifying dragon right now, but if you cut it, it becomes quite...nice (see x). \$\endgroup\$
    – user
    Commented Jul 29, 2021 at 14:35
9
\$\begingroup\$

Vyxal D, 44 bytes

`:qpd4NẎ[20|`:qpd4NẎ[20|`:qpd4NẎ[20|`:qpd4NẎ

Try it Online! Outputs 20.

Left Side:

`:qpd4NẎ[20|`:qpd4NẎ

Try it Online!

Right Side:

[20|`:qpd4NẎ[20|`:qpd4NẎ

Try it Online!

`...`:qpd4NẎ[20|`...`:qpd4NẎ
`...`:qp        `...`:qp       Standard Vyxal quine (of the first 24 bytes)
        d               d      Double ^
         4NẎ             4NẎ   Remove the last 4 characters of ^ to make it a quine
            [20|               If the top of stack is true, push 20, otherwise the second quine.

The D flag allows the character to be used in string literals

\$\endgroup\$
8
\$\begingroup\$

Haskell, 303 bytes

k="\nx=\"k=\"++show k++k++show k\nmain=putStr x where x=\"153\";main=putStr$\"k=\"++show q++q++show q\nq="
x="k="++show k++k++show k
main=putStr x where x="153";main=putStr$"k="++show q++q++show q
q="\nx=\"k=\"++show k++k++show k\nmain=putStr x where x=\"153\";main=putStr$\"k=\"++show q++q++show q\nq="

Try it online!

Prints 153.

k="\nx=\"k=\"++show k++k++show k\nmain=putStr x where x=\"153\";main=putStr$\"k=\"++show q++q++show q\nq="
x="k="++show k++k++show k
main=putStr x where 

Try it online!

x="153";main=putStr$"k="++show q++q++show q
q="\nx=\"k=\"++show k++k++show k\nmain=putStr x where x=\"153\";main=putStr$\"k=\"++show q++q++show q\nq="

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Gol><>, 34 bytes

'r2sslK5+:l)?H1sh"r2sslK5+:l)?H1sh

Try it online!

This is composed of two identical sections of code, except one uses ' and the other uses ". The combined program outputs 17 (which is only one byte away from being able to use a single instruction to push the number), which splits them into practically identical programs.

Try the first half online! And the second half!

Explanation:

'r2ss               Push the code on the first line and add a " to the start
     lK             Push a copy of the stack
       5+           Add 5 to the top " making it a '
         :l)?       If the length of the stack is less than 39
             H      Halt and output the stack contents
              1sh   Otherwise print 17
\$\endgroup\$
2
\$\begingroup\$

Vyxal D, 28 bytes

`Id$ß14`Id$ß14`Id$ß14`Id$ß14

Try it Online! Both parts are identical:

`Id$ß14`Id$ß14
`Id$ß14`       # String "Id$ß14"
        I      # Quote and append, creating this part's source code
         d     # Double, creating the full source
          $ß   # If the previous item on the stack is truthy (the first copy has pushed the source)
            14 # Push 14 (cut point)

An eval-based approach comes out to the same length (both parts identical):

`q‛:Ė+d$[14`:Ė`q‛:Ė+d$[14`:Ė

Try it Online!

`          `:Ė # Evaluate the following string on itself:
 q             # Quote
  ‛:Ė+         # And append ":Ė", resulting in this part's source
      d        # Double, creating the full source
       $[      # If the previous item on the stack is nonempty, i.e. source code pushed
         14    # Push 14

It's possible there's a way to save 4 bytes on the first version (2 on the second) by using how 14 is the length of the halved part, but it seems unlikely.

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

Ruby, 143 bytes

$a=%q[a="$a=%q[#$a];eval$a";$><<a+"b='puts 63;exit';"+a];eval$ab='puts 63;exit';$a=%q[a="$a=%q[#$a];eval$a";$><<a+"b='puts 63;exit';"+a];eval$a

Outputs «63» and exits. First 63 bytes

$a=%q[a="$a=%q[#$a];eval$a";$><<a+"b='puts 63;exit';"+a];eval$a

and the rest

b='puts 63;exit';$a=%q[a="$a=%q[#$a];eval$a";$><<a+"b='puts 63;exit';"+a];eval$a

output the whole quine. I feel like when golfing it inevitably converged towards the existing python solution in the answers. Other approaches seem to all be bigger.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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