The program should start out with 2 separate files, here named "a" and "b". "a" and "b" should be what I am calling inverse quines: "a", when run, should output "b"'s source code, and "b", when run, should output "a"'s source code, for example:
./a
#source code of b, optionally followed by newlines
./b
#source code of a, optionally followed by newlines
If the outputs of "a" and "b" are then appended to the same file, here called "c", such as this:
touch c
./a >> c
./b >> c
The resulting file, "c", when run, should output the code of "a", followed by a newline, followed by the source code of "b" and a newline. The full running of these files should go as follows:
touch c
./a >> c
./b >> c
./c
#source code of a \n
#source code of b \n
Any coding languages can be used, and as this is code golf, the source code should be as small as possible.
There are no limits on what the source code of "a" and "b" can be, and newlines can be placed freely at the end of code outputs, such that the output ends in a single or a string of newlines.
3 Answers 3
Bash, 63 37 bytes
a
t=;x(){ cat 2ドル 1ドル;};x b \
b
${t-cat} a
-26 bytes thanks to Nahuel Fouilleul
quick explanation
There are two key tricks:
- We define our own version of
catcalledxwhich swaps its arguments, when there are 2 arguments given. It will have only 1 argument whenais run, but 2 arguments whencis run. buses bash default arguments to accomplish what it needs. Whentis undefined, as it will be whenbis run alone, it becomescat a. When t is defined, it just returnsa, becoming the second argument toacommandx b a.
-
\$\begingroup\$ Excuse me if I am wrong, but is the first line of "a" needed? The program seems to run fine without it for me, and there is no difference in error codes, so if it is neccecary, could someone please explain why? Thanks. \$\endgroup\$blueberry– blueberry2021年02月16日 08:20:38 +00:00Commented Feb 16, 2021 at 8:20
-
\$\begingroup\$ @Jake I will add a quick explanation, but without it the output of c will be wrong. \$\endgroup\$Jonah– Jonah2021年02月16日 08:23:29 +00:00Commented Feb 16, 2021 at 8:23
-
2\$\begingroup\$ Note that this solution reads its own source code, which is not allowed under standard quine rules. \$\endgroup\$user202729– user2027292021年02月16日 08:35:45 +00:00Commented Feb 16, 2021 at 8:35
-
\$\begingroup\$ (originally there was no quine tag on the challenge but I added it because it seems relevant) \$\endgroup\$user202729– user2027292021年02月16日 08:36:03 +00:00Commented Feb 16, 2021 at 8:36
-
1\$\begingroup\$ Seeing as these are not technically quines, although very similar, I would say that the use of
catis fine. \$\endgroup\$blueberry– blueberry2021年02月16日 09:07:51 +00:00Commented Feb 16, 2021 at 9:07
05AB1E, 39 (20+19) bytes
Program a:
"34çìD«r» ̈"34çìD«r» ̈
Program b:
"34çìD«r» ̈"34çìD«r»
Program c (concatenation of a+b):
"34çìD«r» ̈"34çìD«r» ̈"34çìD«r» ̈"34çìD«r»
Try it online or try it online as pure quine.
Explanation:
Program a:
"34çìD«r» ̈" # Push string '34çìD«r» ̈'
34 # Push 34
ç # Convert it to a character: '"'
ì # Prepend it in front of the string: '"34çìD«r» ̈'
D # Duplicate it
« # Merge the copy to itself: '"34çìD«r» ̈"34çìD«r» ̈'
r # Reverse the items on the stack
# (no-op, since there is just a single item)
» # Join all items on the stack by newlines
# (no-op, since there is just a single item)
̈ # Remove the last character: '"34çìD«r» ̈"34çìD«r»'
# (after which the result is output implicitly)
Program b:
The same as program a, but without the trailing ̈, so it'll output "34çìD«r» ̈"34çìD«r» ̈ (being program a) instead.
Program c:
"34çìD«r» ̈"34çìD«r» ̈ # It starts the same as program a
"34çìD«r» ̈" # Push string '34çìD«r» ̈'
# STACK: ['"34çìD«r» ̈"34çìD«r»', '34çìD«r» ̈']
34çìD« # Do the same as above
# STACK: ['"34çìD«r» ̈"34çìD«r»', '"34çìD«r» ̈"34çìD«r» ̈']
r # Reverse the items on the stack
# STACK: ['"34çìD«r» ̈"34çìD«r» ̈', '"34çìD«r» ̈"34çìD«r»']
» # Join all items on the stack by newlines
# STACK: ['"34çìD«r» ̈"34çìD«r» ̈\n"34çìD«r» ̈"34çìD«r»']
# (after which the result is output implicitly)
Ruby, 60 * 2 bytes
This is essentially a quine relay but within one language. Here is a.rb:
eval s=%q[1;puts "eval s=%q[#{s[0][?1]&&?2||?1}#{s[1..]}]"]
The b.rb is only different in one symbol:
eval s=%q[2;puts "eval s=%q[#{s[0][?1]&&?2||?1}#{s[1..]}]"]
Testing:
$ ruby b.rb > c.rb
$ diff c.rb a.rb
$ ruby a.rb > c.rb
$ diff c.rb b.rb
$ ruby b.rb >> c.rb
$ ruby c.rb
eval s=%q[1;puts "eval s=%q[#{s[0][?1]&&?2||?1}#{s[1..]}]"]
eval s=%q[2;puts "eval s=%q[#{s[0][?1]&&?2||?1}#{s[1..]}]"]
aandbare identical? \$\endgroup\$aalone outputs source ofband a newline, and vice versa? \$\endgroup\$