Bidirectional Quine Chain

9

1

PPCG hasn't had enough of quines already...

Challenge:

Your task is to create a program "A0". When this program is run with no input, it outputs nothing. When this program is run with input, it outputs "A1". When "A1" is run with no input, it outputs "A0". When "A1" is run with input, it outputs "A2". Pretty much, "A(k)" will output "A(k-1)" when run with no input, and will output "A(k+1)" when run with input.

Details

I believe this challenge is simple enough; there are no other rules really. Every program must contain at least 1 byte, by the way. You may assume that the input will consist of only ASCII characters, and you may ignore whitespace if you want, but you may not specify a specific input. Output may be to either STDOUT or STDERR, but all of your programs must output to the same one. The other one may also contain text (so you may output to STDOUT and then exit with an error). Thanks to @Dennis for pointing that out.

All programs must be in the same language, and each program must be unique from the rest of them.

The score is equal to the length of program "A0". As this is a code-golf challenge, the lowest score wins!

HyperNeutrino

Posted 2017-02-24T00:03:26.390

Reputation: 26 575

I am sad to predict that most submissions will have A(k) and A(k+1) differing by a single character being added to a growing string :( – Sparr – 2017-02-24T00:06:44.930

@Sparr Unfortunately, that is probably going to be what happens. :( Oh well, I can't figure out how to make a clear rule against that. – HyperNeutrino – 2017-02-24T00:08:19.637

you can only output to one of the two I'm not dure if I'm interpreting this correctly. If we print the desired output to STDOUT, does STDERR have to be empty? Because it is udually allowed to exit with an error. – Dennis – 2017-02-24T01:20:31.020

Must all the programs be different? The question doesn't say that. – None – 2017-02-24T01:41:31.333

@Dennis Yes, that was my intention. Is that the case? I'm not too familiar with the rules here, so I guess I'll change the specs to follow standards. Thanks for pointing that out! – HyperNeutrino – 2017-02-24T01:42:13.873

@ais523 That is not a specification. However, it is impossible to write a valid solution where all programs are the same, because "A0" and "A1" behave differently when given no input; A0 prints nothing but A1 prints A0. So, in short, yes, but not because I said so, it's because it doesn't work otherwise. – HyperNeutrino – 2017-02-24T01:43:10.497

@ais523 I also clarified (which I forgot to do earlier) that all programs must be non-empty. So sorry if that messes up your solution, but that's a potential loophole that I forgot to specify. If you have a solution that exploits this loophole, go ahead and submit it, and I'll give it an upvote, but I won't consider it to be valid. – HyperNeutrino – 2017-02-24T01:47:32.823

@HyperNeutrino Please don't encourage invalid answers. Invalid answers should and will be deleted.

– Martin Ender – 2017-02-24T08:49:24.307

4Also, may I suggest a somewhat more expressive title like "Bidirectional Quine Chain"? "super meta quine" doesn't say a whole lot beyond programs printing other programs and will make it harder to search for this challenge in the future. – Martin Ender – 2017-02-24T08:52:05.797

Are Ai and Aj required to be in the same language? – NonlinearFruit – 2017-02-27T20:49:18.403

@MartinEnder I hope my most recent edit fixed that issue. – HyperNeutrino – 2017-02-28T02:43:07.530

@MartinEnder And also, thanks for the title suggestion; I've made the change. – HyperNeutrino – 2017-02-28T02:43:25.833

@NonlinearFruit Yes. I will clarify that in the question. Thanks! – HyperNeutrino – 2017-02-28T02:43:46.640

Is the chain supposed to continue indefinitely, i.e. there's an A(k) for all k >= 0? – DLosc – 2017-02-28T09:12:21.713

@DLosc The scoring used to be length of program "A0" in bytes but the most recent edit removed that... – NonlinearFruit – 2017-02-28T14:30:42.033

@DLosc Added that back in. Thanks for noticing. – HyperNeutrino – 2017-02-28T16:01:39.210

Related – mbomb007 – 2017-02-28T16:15:04.820

Answers

1

Pip, 28 bytes

V Y"I#qSti0+i?`V Y`.RPyRtiu"

Try it online!

Explanation

This is a modified version of the shortest known Pip quine V Y"`V Y`.RPy". That quine works by defining a string, yanking it into the y variable, and then evaluating it. When evaluated, the string takes the repr of y (thus wrapping the value of y in double quotes) and concatenates the pattern literal `V Y` to the front of it.

Our strategy is to put a 0 in the program, then replace 0 with 10 if there was input, or replace 10 with 0 if there was no input. (Thus, A(k) will contain a number consisting of k 1's followed by a 0.) 0 and 10 are convenient because there are built-in variables (i and t, respectively) with those values, so we can refer to them without using actual digits.

So instead of RPy, we want RP yRit if there was input and RP yRti if not. We can combine the two cases by swapping the values of t and i if there is input (I#q Sti), then doing RP yRti. (We have to test #q, the length of the input, because inputs like 0 are falsey.)

Now we just have to get a literal 0 in the code and handle the special case of A0 producing no output. Both can be solved by testing 0+i and returning u if it is falsey:

  • For any k > 0, the number in A(k) will be nonzero and therefore truthy (e.g. 110+i).
  • For k = 0, the number in A(k) will be zero:
    • If there is input, i and t are swapped and i is 10. 0+i is still truthy.
    • If there is no input, i is still 0 and 0+i is falsey. Instead of the quine core, we output u, which is a built-in variable for nil. Printing nil produces no output.

DLosc

Posted 2017-02-24T00:03:26.390

Reputation: 21 213

Good job! Works like a charm. – HyperNeutrino – 2017-03-01T02:25:27.180

1

Python 2, 93 bytes

There is a trailing linefeed.

p=1+2*bool(input())-1;s='print"p=%r+2*bool(input())-1;s=%r*(p>0);exec s"%(p,s)'*(p>0);exec s

Try it with input | Try it without input

This is modified from my answer on a similar question.

If there is input, then it will increment p. So the resulting program will be p=2+..., p=3+..., etc.

mbomb007

Posted 2017-02-24T00:03:26.390

Reputation: 21 944

This doesn't recognize 0 as input – fəˈnɛtɪk – 2017-02-28T20:18:25.757

@LliwTelracs Input has to be surrounded in quotation marks (it has to be a sting). See the hyperlinks in the answer. – mbomb007 – 2017-02-28T20:27:56.583

As it is, your program works with strings and all numbers other than 0. – fəˈnɛtɪk – 2017-02-28T20:30:56.283

1@LliwTelracs Well don't try to use numbers then. According to consensus, I can use input() and require input to be surrounded by quotes, rather than using raw_input(). If you want to input zero, use "0". – mbomb007 – 2017-02-28T20:31:46.917

Nice solution! I was expecting most answers to be infinitely growing in length (technically this one does but not in the same sense). Good job! – HyperNeutrino – 2017-03-01T02:24:41.593