Make a quine but with a twist.


The quine prints its code but puts its first character at the end.
(You can do it in reverse but include that note in your answer)
The output must then be a program which is also an solution.

Example: Assume your code was foobar, running it will return oobarf which will be another valid program.

foobar -> oobarf
oobarf -> obarfo
obarfo -> barfoo
barfoo -> arfoob
arfoob -> rfooba
rfooba -> foobar


  • Your code must not be an output of some generation of someone else's code that's quite obviously stealing
  • Your code must be more than 2 characters long (so short code is no fun)
  • Your code must contain at least two different characters (ex: +++ is not valid)


As a challenge the shortest code wins.

  • 2
    \$\begingroup\$ Well that's upsetting: codegolf.meta.stackexchange.com/a/14104/9365 \$\endgroup\$ Apr 25, 2018 at 19:07
  • 3
    \$\begingroup\$ @DomHastings You've had months to post that. \$\endgroup\$
    – mbomb007
    Apr 25, 2018 at 19:13
  • 2
    \$\begingroup\$ @IQuick143 I think >2 characters is a good requirement for the reason you stated \$\endgroup\$
    – dylnan
    Apr 25, 2018 at 20:00
  • 5
    \$\begingroup\$ Would this count as a 'rotating quine'? ;p \$\endgroup\$ Apr 26, 2018 at 7:48
  • 3
    \$\begingroup\$ @mbomb007 Finding a language that this is possible in is a part of the challenge \$\endgroup\$
    – IQuick 143
    May 2, 2018 at 19:56

1 Answer 1


Befunge-98 (PyFunge), 2600 bytes


Try it online!

This making this was a hellfest.

How this works:
The program is a bunch of put statements that assembles a Program B around itself which then prints half of the source byte shifted twice.
The program is actually 2 copies of a 1300 byte program, this is so it's ensured that the whole 1300 byte program always gets ran as a whole.

Better explanation:
Every Befunge-98 quine needs to contain symbols such a @ or q and ,
Problem: None of those symbols are a good starting point especialy since @ and q terminate the program instantly.
Solution: Get rid of those characters in the source code

Problem: How?
Solution: Use p (put) commands to modify the source code to include the required characters which will print the contains of the source code shifted by one byte and not use the g command which is cheating.

Problem: (sigh when will these end)
A put command pops 3 values n x y which determine character, x-coord, y-coord however when the initialization of these values is split in half it can write bad characters in the beginning source code making it useless for quining.
Solution: (last one I promise)
Use 2 copies of the source code, the latter one being the "correct one" this accidentally fixes another problem which is that a put statement (p command + the constant initializers) which is split in half will not get executed, this is fixed by having 2 copies of each statement. Last thing this needs to work is how do we make the whole source code out of a half?

An image's worth 1000 words they say. Then they made Piet.

This is visual proof of why two copies of a string byte shifted == Two copies of a byte shifted string. That means we can take a half of the code, byte shift it, then print it twice (OR take half of the code, byte shift it, print, repeat [That's what actually happens])

How this is implemented: Assume 0123456789abcdef is the source

Befunge Pseudocode:

0123456789abcv;;"123456789abcdef" < go this way <--
             >PS'0,   repeat 2x   ^

PS means Print Stack (not a real instruction). We push half of the source code in reverse onto the stack using "" then we print the stack and then we fetch (' command) the first character 0 which we move in front of the ' and print it last which causes the byte shift, then we repeat the cycle once more to print the second copy. One technicality to deal with are the symbols inside the source, this can cause trouble if we write it while executing the source code, I circumvented this by adding more put statements which take care of it externally.

This makes the code look something like this: Code

Green Highlight: Code that takes care of adding characters into the source
Grey Letters (probs poor visibility sorry): Code that gets added by green code
Red Highlight: Code that moves first character of the second half of source code into the Blue area.
Blue Highlight: See Red Highlight
Orange Highlight: Code that makes sure we terminate after we wrote 2 byte shifted copies by putting a @ (terminate) command into the Yellow Area.

Arrows should hopefully make it clearer how the code flow goes.

Here comes the last tough part:
Where do babies source code come from?

Short answer: C# Magic
Long answer: 100+ Befunge code snippets made by hand compiled by C# code. I manually wrote about 100 constant initializers (a piece of befunge code that pushes a certain number to stack) by hand and then used a custom C# program to compile it into the 1300 byte Befunge output, which I then copy pasted twice and made the final program.

Are you still here? Great thank you for reading! (or at least scrolling to the end)
I hope my bad jokes were fun and not annoying.

Note: No put statement in this code creates a g command which would be cheating.

EDIT: I have verified the code using the following Javascript code in TIO using developer tools

setInterval(()=>{document.getElementById("code").value = 
document.getElementById("run").click();}, 2000);
  • \$\begingroup\$ I'm really looking forward to the full explanation, but this seems like quite an achievement and certainly deserves the bounty! \$\endgroup\$
    – N. Virgo
    May 6, 2018 at 11:59
  • 1
    \$\begingroup\$ @Nathaniel There goes the full Explanation hope you like it! :D \$\endgroup\$
    – IQuick 143
    May 6, 2018 at 16:37
  • 1
    \$\begingroup\$ That part about dealing with p with incorrect arguments is genius. \$\endgroup\$
    – leo3065
    May 6, 2018 at 16:49
  • \$\begingroup\$ @lQuick indeed I do! \$\endgroup\$
    – N. Virgo
    May 8, 2018 at 1:25
  • \$\begingroup\$ Nice answer! 6:*:*4+ can be aa*d*. Do you still have a list of the initializers? \$\endgroup\$
    – ovs
    May 8, 2018 at 10:06

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.