9
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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 :( \$\endgroup\$ – Sparr Feb 24 '17 at 0:06
  • \$\begingroup\$ @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. \$\endgroup\$ – HyperNeutrino Feb 24 '17 at 0:08
  • \$\begingroup\$ 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. \$\endgroup\$ – Dennis Feb 24 '17 at 1:20
  • \$\begingroup\$ Must all the programs be different? The question doesn't say that. \$\endgroup\$ – user62131 Feb 24 '17 at 1:41
  • 4
    \$\begingroup\$ Also, 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. \$\endgroup\$ – Martin Ender Feb 24 '17 at 8:52
1
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Good job! Works like a charm. \$\endgroup\$ – HyperNeutrino Mar 1 '17 at 2:25
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This doesn't recognize 0 as input \$\endgroup\$ – fəˈnɛtɪk Feb 28 '17 at 20:18
  • \$\begingroup\$ @LliwTelracs Input has to be surrounded in quotation marks (it has to be a sting). See the hyperlinks in the answer. \$\endgroup\$ – mbomb007 Feb 28 '17 at 20:27
  • \$\begingroup\$ As it is, your program works with strings and all numbers other than 0. \$\endgroup\$ – fəˈnɛtɪk Feb 28 '17 at 20:30
  • \$\begingroup\$ @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". \$\endgroup\$ – mbomb007 Feb 28 '17 at 20:31
  • \$\begingroup\$ 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! \$\endgroup\$ – HyperNeutrino Mar 1 '17 at 2:24

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.