Codegolf: Quine and Antiquine

13

This challenge is similar to Can you Meta Quine?

A quine is a program that produces itself on STDOUT. This challenge is to produce a program A which when run produces a program B on STDOUT. Program B when run produces program A on STDOUT. Programs A and B must be written in (and run in) the same language. The linked question constrained A != B. That looked too easy. So for this question, we insist A and B are antiquines, using the following rules:

  1. Programs A and B may not use any of the same characters, save for whitespace and statement separators, and punctuation characters.
  2. Programs A and B must each contain at least one character that is neither whitespace nor a statement separator, nor a punctuation character.
  3. For the purpose of rules 1 and 2, the term 'whitespace' excludes any symbol or sequence of symbols which itself is a statement, operator or symbol that is interpreted (as opposed to a separator). Therefore in the Whitespace language, there is no whitespace.
  4. A statement separator is a syntactic element conventionally used within the language to separate statements. This would include the newline in python, or the semicolon in Java, perl or C.
  5. A punctuation character is an ASCII character which is neither whitespace nor in the POSIX word character class (i.e. an underscore is not punctuation for this purpose) - i.e ispunct() would return true, and it's not _.
  6. Program A when run must produce a program (Program B) on its STDOUT, which when run in turn produces Program A.
  7. Programs A and B must be in the same programming language.
  8. The programming language used must actually be a programming language. Unless you make a good case otherwise, I'll suggest it must be Turing complete.
  9. At least one of A and B must execute at least one statement within the language.

This is code golf, so the shortest answer wins, the score being the length of program A in bytes (i.e. the length of program B is not relevant).

abligh

Posted 2014-08-16T19:17:58.730

Reputation: 777

Can you please add "punctuation" to the list of exceptions in the first clause? It's impossible to solve this task in many languages (especially those that require you to you curly braces in the main function) otherwise. – FUZxxl – 2014-08-16T19:21:11.180

related but probably sufficiently different. – Martin Ender – 2014-08-16T19:23:12.027

@FUZxxl I've permitted punctuation marks but excluded underscores. – abligh – 2014-08-16T19:32:55.280

So just to make sure: Things like ' and " are allowed to appear in both programs, correct? – Ingo Bürk – 2014-08-16T20:05:34.703

@IngoBürk, yes, thinks like ' and " can appear in both programs (per the exception at the end of rule 1). However there must be something that appears in each program that is NOT a punctuation mark, a whitespace character or a statement separator. IE you can't just produce a single quine made of punctuation marks, and say it is both A and B. – abligh – 2014-08-16T20:15:35.787

Can you use space/tab in python? It's part of the statement separator, just not always. – isaacg – 2014-08-17T09:35:35.570

Your definition of whitespace is wrong. print("Hello, World!") is not the same as pri nt("Hello, World!"). – Doorknob – 2014-08-17T09:47:57.103

I guess Python is out. Can't imagine any of A or B not having a print command, causing both programs containing these 5 characters. (sys.stdout.write contains r, i and t as well.) – Falko – 2014-08-17T16:32:49.563

@isaacg you can use whitespace in any language (including Python), but there must also be non-whitespace characters. – abligh – 2014-08-17T16:58:23.537

@Doorknob thanks - I've clarified that. – abligh – 2014-08-17T17:01:44.647

1For the purposes here, are A and a considered different "characters"? – HostileFork says dont trust SE – 2014-08-17T17:33:44.230

1It would be better if you provided a complete list of ASCII characters that match rule 5. – aditsu quit because SE is EVIL – 2014-08-17T18:37:03.333

So, if A has 'class', B would have to use 'CLASS'? – Οurous – 2014-08-17T22:52:00.693

@Ourous: If A has 'class', B would have to have something other than class; CLASS is but one option. – abligh – 2014-08-17T23:47:29.693

@aditsu: the POSIX word character class consists of the letters a-Z, A-Z, the numbers 0-9 and underscore (_). – abligh – 2014-08-17T23:48:24.663

@abligh that's not what I asked.. – aditsu quit because SE is EVIL – 2014-08-17T23:57:10.630

@aditsu I've made the rule rather simpler (similar to the definition of ispunct()). – abligh – 2014-08-18T07:40:49.413

Answers

5

GolfScript, 13 bytes

1{\~\".~"}.~

The output is

-2{\~\".~"}.~

which generates the initial program.

The byte count include the trailing LF, since the output of the output will contain it.

Try it online.

How it works

1       # Push 1.
{       # Start code block.
  \~\   # Apply logical NOT to the second topmost element of the stack.
  ".~"  # Push that string.
}       # End code block.
.~      # Duplicate the code block and execute the copy.

GolfScript prints the stack's contents upon termination.

Dennis

Posted 2014-08-16T19:17:58.730

Reputation: 196 637

Please read the problem statement again - Programs A and B may not use any of the same characters, save for whitespace and statement separators, and punctuation characters. – isaacg – 2014-08-18T16:42:15.103

1Everything but 1 and 2 is punctuation. – Dennis – 2014-08-18T16:44:24.320

OK, the rules were changed since I last read them. – isaacg – 2014-08-18T16:46:56.563

Yup, that works. I'd phrased the rules to prevent whitespace being used as statements, but forgot the same loophole would be present for punctuation. So loophole legally exploited. – abligh – 2014-08-18T16:59:51.987

14

Pascal (731 characters)

Program A:

program s;{$h+}uses sysutils;const p='program s;{$h+}uses sysutils;const p=';a='a';aa=''';';aaa='a=''';aaaa='''';aaaaa='begin write(lowercase(p+aaaa+p+aa+aaa+a+aa+a+aaa+aaaa+aa+aa+a+a+aaa+aaa+aaaa+aa+a+a+a+aaa+aaaa+aaaa+aa+a+a+a+a+aaa+stringreplace(stringreplace(stringreplace(stringreplace(aaaaa,aaaa,aaaa+aaaa,[rfreplaceall]),''lower''+''c'',''tm''+''p'',[]),''up''+''c'',''lower''+''c'',[]),''tm''+''p'',''up''+''c'',[])+aa+aaaaa))end.';begin write(upcase(p+aaaa+p+aa+aaa+a+aa+a+aaa+aaaa+aa+aa+a+a+aaa+aaa+aaaa+aa+a+a+a+aaa+aaaa+aaaa+aa+a+a+a+a+aaa+stringreplace(stringreplace(stringreplace(stringreplace(aaaaa,aaaa,aaaa+aaaa,[rfreplaceall]),'lower'+'c','tm'+'p',[]),'up'+'c','lower'+'c',[]),'tm'+'p','up'+'c',[])+aa+aaaaa))end.

Outputs program B:

PROGRAM S;{$H+}USES SYSUTILS;CONST P='PROGRAM S;{$H+}USES SYSUTILS;CONST P=';A='A';AA=''';';AAA='A=''';AAAA='''';AAAAA='BEGIN WRITE(UPCASE(P+AAAA+P+AA+AAA+A+AA+A+AAA+AAAA+AA+AA+A+A+AAA+AAA+AAAA+AA+A+A+A+AAA+AAAA+AAAA+AA+A+A+A+A+AAA+STRINGREPLACE(STRINGREPLACE(STRINGREPLACE(STRINGREPLACE(AAAAA,AAAA,AAAA+AAAA,[RFREPLACEALL]),''LOWER''+''C'',''TM''+''P'',[]),''UP''+''C'',''LOWER''+''C'',[]),''TM''+''P'',''UP''+''C'',[])+AA+AAAAA))END.';BEGIN WRITE(LOWERCASE(P+AAAA+P+AA+AAA+A+AA+A+AAA+AAAA+AA+AA+A+A+AAA+AAA+AAAA+AA+A+A+A+AAA+AAAA+AAAA+AA+A+A+A+A+AAA+STRINGREPLACE(STRINGREPLACE(STRINGREPLACE(STRINGREPLACE(AAAAA,AAAA,AAAA+AAAA,[RFREPLACEALL]),'LOWER'+'C','TM'+'P',[]),'UP'+'C','LOWER'+'C',[]),'TM'+'P','UP'+'C',[])+AA+AAAAA))END.

Outputs program A.

Mika Lammi

Posted 2014-08-16T19:17:58.730

Reputation: 1 151

3I am enjoying the (mis)use of case translation. – abligh – 2014-08-17T23:49:54.590

11

ROT13 (not competing anymore after rule update)

Not sure if this counts as a language, but I certainly didn't make it up for the challenge. Usually answers for certain utilities such as sed are accepted as well. However, it's a judgement call, so if it doesn't count I'll remove it (is there a meta discussion somewhere on what counts as a language? Edit: There is now)

A

Cycles between A and N:

~>> echo "A" | rot13
N
~>> echo "A" | rot13 | rot13
A

Ingo Bürk

Posted 2014-08-16T19:17:58.730

Reputation: 2 674

I think this doesn't work, as rot13 is simply taking input on STDIN, not executing a program. If you are arguing that it is a zero length program, that fails to satisfy rule 2. The program (with sed) is what's passed in sed -e. If this was acceptable, then cat would be a quine, and tac would be a quine and antiquine. However, nothing is producing the source to cat, tac or for that matter rot13. – abligh – 2014-08-16T19:42:15.140

@abligh If rot13 is the language used, why should I have to print the source code for rot13? Also, if I run Golfscript, Python or anything else, I will likely just pass the source code to it via stdin or file, so there isn't really any difference. In this case rot13 is the interpreter. – Ingo Bürk – 2014-08-16T19:45:32.133

rot13 as a programming language does not appear to satisfy any reasonable definition of 'programming language', and it's not interpreting anything. I will amend the question to make that clearer. – abligh – 2014-08-16T19:49:37.827

Fine with me. I'll leave this answer as not competing anymore and start a meta discussion. Nice challenge, by the way. Can't wait to see "real" solutions! – Ingo Bürk – 2014-08-16T19:54:11.047