Cheating Cyclic Quine

20

2

Concept

Write a program that outputs code in its programming language. That code, when executed, must output the original program.

Rules

  • Since this is a cheating quine, you can read the original source code.
  • First output program must be in the same language as the original program.
  • You may not output a regular quine. The two programs must be different.
  • Standard loopholes apply.
  • This is so shortest answer wins, however it will not be selected.

dkudriavtsev

Posted 2016-12-11T23:33:28.313

Reputation: 5 781

I'm still not entirely sure what constitutes a semi-quine. – Conor O'Brien – 2016-12-11T23:36:43.347

@ConorO'Brien It outputs a program that outputs the original program – dkudriavtsev – 2016-12-11T23:37:12.173

1I see. The wording is quite vague. – Conor O'Brien – 2016-12-11T23:40:36.453

2Why wont the shortest answer be selected? Is this a Catalog? – ATaco – 2016-12-12T00:38:31.373

We already have another definition for semiquine. This is a cyclic quine. Related. – Mego – 2016-12-12T00:45:16.727

I'd call it square root of quine. (Because I like how quantum computing has the operator square root of not.) – Christian Sievers – 2016-12-12T00:52:55.360

Really, the only differences between this and the challenge I linked are that cheating quines are allowed and the chain needs to be of length 2. I'm not going to VTC this as a dupe (since my vote is a hammer), but I will put forward the proposal that this might be a dupe. – Mego – 2016-12-12T06:46:16.750

2

Just for the record, I'd call it a mutual quine (that challenge requires different languages though).

– Martin Ender – 2016-12-12T08:02:59.370

@Mego I'll give my two cents, This is not a dupe as this is Code-Golf, not Code-Challenge. The Quine Permutation Group challenge asks for a very different solution, as is obvious by Dennis's. – ATaco – 2016-12-13T05:08:43.193

@ATaco The other challenge scored based on code length and chain length. With the exception of the few solutions that are infinitely extendable, most of the chain lengths were small, so the score was dominated by code length. – Mego – 2016-12-13T13:09:50.137

Since this is a cheating quine, can we choose to not follow the rules? – Keeta - reinstate Monica – 2016-12-13T18:58:49.317

@Keeta No, cheating only means that you can read the source code – dkudriavtsev – 2016-12-13T19:10:17.203

Answers

28

Bash + coreutils, 11 bytes

tr xy yx<$0

This prints

tr yx xy<$0

Try it online!

In turn, that prints

tr xy yx<$0

Dennis

Posted 2016-12-11T23:33:28.313

Reputation: 196 637

1Not a cheat and in a common language. – denson – 2016-12-12T08:49:52.943

8I think the tr command is redirected to $0 which is its own source code. So it is "cheaty". – Roman Gräf – 2016-12-12T12:04:40.677

21

Snails, 0 bytes



The second program is

1

The first program counts the number of matches of the empty pattern on the empty input (which really has area 0, but the pattern is always run at least once as a hack to allow programs to decide what they want to print on empty input). The second program begins with a quantifier (like {1} in regex), which causes a parse error. Since the program does not parse successfully, STDOUT is the empty string.

feersum

Posted 2016-12-11T23:33:28.313

Reputation: 29 566

I thought there was probably a language that did this (i.e. null program succeeds with a nonempty result, but that result does nothing as a program). I tried HOMESPRING but couldn't get the interpreter to work. Looks like you found another language where it works, though. (Do you have a link to an interpreter?) – None – 2016-12-12T23:47:06.800

@ais523 https://github.com/feresum/PMA or https://tio.run/nexus/snails%E2%80%8B.

– feersum – 2016-12-12T23:49:06.507

20

7, 2 bytes

7 uses a 3-bit character set, but takes input packed into bytes (and according to meta, languages with sub-byte character sets are counted using bytes for the file on disk). Here's an xxd dump of the program:

00000000: 4cf4                                     L.

When giving this file to the 7 interpreter, it will output the following program:

00000000: 4fa6 7f                                  O..

which will then in turn output the original program again.

So what's happening here? There's no source reading involved (actually, I don't think it's possible to read the source in 7), although arguably the program is cheating in another way; let me know what you think. Here's how the program works. (Note that each 7 command has two variants, some of which don't have names and can't appear in the original program. There are twelve commands total, in six pairs. I'm using bold for active commands, nonbold for passive commands, and in cases where the active command has no name, I'm giving it the same name as the corresponding passive command and relying on the bold to distinguish. In the case where both are named, e.g. 7 which is the active variant of 1, each command gets its own name and the bold is just syntax highlighting.)

231723       Original program, unpacked into octal
231          Push 237 onto the stack
    23       Push 23 onto the stack
             (implicit) append a copy of the top of stack to the program
      2      Duplicate top of stack (currently 23)
       3     Output top of stack, pop second stack element

At this point, the 7 interpreter sees that the top of stack contains commands (2 and 3) that aren't representable, so it escapes the top of the stack, producing 723 (which is). The first command output selects the output format; in this case, it's format 7, "format the output the same way as the program". So the commands get output packed into bytes. Then the program continues:

23172323
             (implicit) append a copy of the top of stack to the program
        2    Duplicate top of stack (currently 237)
         3   Output top of stack, pop second stack element
          7  Push an empty element onto the stack

At this point, there's nothing but empty stack elements on the stack, so the program exits. We output 23 earlier. If we escape 237 (and we have to, because it contains unrepresentable commands), we get 7231. That gets output directly, making the final output of the program 237231 (formatted the same way as the program, i.e. packed into bytes). That's 4fa67f. (It can be noted that the 1 was entirely pointless in terms of affecting the output; the only reason it's there is to make the two programs different.)

Running 237231 proceeds almost exactly the same way; the difference is that the useless 1 runs just after the first print (and the empty element gets implicitly deleted the second time the current end of the program is reached). Again, the 231 ends up outputting itself, the 23 ends up outputting itself preceded by a 7, and we get 231723, the original program.

The observant might note that the two programs, despite being the same length in the language's "native" octal, are different lengths on disk. That's because a 7 program can be padded with an arbitrary number of 1 bits, and the packed format discards the trailing padding. Here's how the encoding happens:

2  3  1  7  2  3
010011001111010011(1...)
4   c   f   4   padding

In other words, two bytes, 4C F4, are enough to represent the program, so that's all I used.

user62131

Posted 2016-12-11T23:33:28.313

Reputation:

11

Python 3, 297 279 251 243 225 218 208 180 126 111 bytes

Non-cheating:

A=''';print(("A="+("'"*3+A)*2).translate({65:66,66:65}))''';print(("A="+("'"*3+A)*2).translate({65:66,66:65}))

This prints:

B=''';print(("B="+("'"*3+B)*2).translate({65:66,66:65}))''';print(("B="+("'"*3+B)*2).translate({65:66,66:65}))

which, when executed, prints the original program.

L3viathan

Posted 2016-12-11T23:33:28.313

Reputation: 3 151

Wow! Cool! I never would have thought of that. – dkudriavtsev – 2016-12-11T23:43:01.760

8

RProgN, 4 Bytes.

The community seems to consider this sort of thing as a cheating quine, which thus satisfies the criteria.

1
2

With a trailing newline.

This prints

2
1

With a trailing newline, which prints the first code.

RProgN prints the stack popping, so from top to bottom.

Try it online!

ATaco

Posted 2016-12-11T23:33:28.313

Reputation: 7 898

There was a chat discussion a while back which implied that 1\n1\n in RProgN would be a non-cheating quine, because each of the 1s prints each other (and it'd only be considered cheating if each 1 printed itself). All this really implies is that cheating in quines can be hard to define sometimes. (However, this answer is correct either way, because the question doesn't actually require the quine to cheat, just allows it.) – None – 2016-12-12T23:49:40.453

Also, Although I was reluctant to say, in RProgN 1\n is technically a valid quine, as 1 isn't a constant, but a call to a function which pushes 1 to the stack. – ATaco – 2016-12-13T00:02:32.477

There are multiple definitions of a proper quine in use. That's invalid by at least one, but possibly valid by some of the others. – None – 2016-12-13T00:06:35.290

@ais523 You've sparked my curiosity, You specified RProgN, yet I didn't think that my little language got that much (or any) attention. Was the chat about RProgN in particular, or a language with a similar syntax? – ATaco – 2016-12-13T04:53:49.360

It was about 7, which also has a tendency to print segments of the program "backwards". I was extrapolating from the opinions given there.

– None – 2016-12-13T06:24:08.193

8

Batch, 14 bytes

@echo @type %0

Which when run as cyclicquine.bat outputs

@type cyclicquine.bat

Which when run outputs the original batch file.

Neil

Posted 2016-12-11T23:33:28.313

Reputation: 95 035

6

Jolf, 6 bytes

1q_a_q

When run, this outputs:

q_a_q1

Which in turn outputs 1q_a_q.

Try it here!

Explanation

1q_a_q
1       the digit one (ignored)
 q      the source code (ignored)
     q  the source code
    _   reversed
   a    and outputted
 _      and reversed (ignored)

q_a_q1
q       the source code (ignored)
     1  the digit one (ignored)
    q   the source code
   _    reversed
  a     and outputted
 _      and reversed (ignored)

Conor O'Brien

Posted 2016-12-11T23:33:28.313

Reputation: 36 228

5

JavaScript (ES6), 69 60 59 bytes

(_0=_=>console.log(`(_0=${_0})()`.replace(/0/g,n=>+!+n)))()

Outputs:

(_1=_=>console.log(`(_1=${_1})()`.replace(/1/g,n=>+!+n)))()

-1 byte (@ETHProductions): use 0 in regex instead of \d

darrylyeo

Posted 2016-12-11T23:33:28.313

Reputation: 6 214

Try n=>1-n instead of n=>+!+n. – Conor O'Brien – 2016-12-12T13:01:12.217

@Conner O'Brien Unfortunately the 1 will end up replaced by the regex. – darrylyeo – 2016-12-12T15:24:47.070

@ETHproductions Heh, I should've thought of that. – darrylyeo – 2016-12-12T20:55:34.047

3

Bash + Sed, 15 bytes

Same idea as Dennis's Answer.

sed y/xz/zx/ $0

NoOneIsHere

Posted 2016-12-11T23:33:28.313

Reputation: 1 916

3

Bash, Cat, and Rev, 19 16 bytes

rev $0 # 0$  ver

-3 thanks to @izabera

NoOneIsHere

Posted 2016-12-11T23:33:28.313

Reputation: 1 916

Wouldn't this be Bash, Tac, and `Rev? – Conor O'Brien – 2016-12-12T13:00:28.873

rev $0 # 0$<space><space>ver for some reason i can't put two spaces in a comment – izabera – 2016-12-12T13:09:57.523

Oh, nice @izabera – NoOneIsHere – 2016-12-12T17:07:30.993

@ConorO'Brien No, tac rev-ed is cat. – NoOneIsHere – 2016-12-12T17:07:50.940

@SeeOneRhino Oh.... I see – Conor O'Brien – 2016-12-12T18:15:33.477

1

><>, 16 bytes

"$r00gol?!;50.01

Try it here!

Outputs

"$r00gol?!;50.10

This is a modification of the one provided here. I'm not sure if it's cheating, it reads the codebox and outputs it.

redstarcoder

Posted 2016-12-11T23:33:28.313

Reputation: 1 771

1you can change the 00g to :2- and it'll keep the same byte count without reading the codebox. – Teal pelican – 2016-12-13T10:26:02.987

@Tealpelican, thanks! The goal is for it to be considered "cheating" though :p. (And I don't fully understand that rule) – redstarcoder – 2016-12-13T15:02:40.203