12
\$\begingroup\$

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.

Bubbler
79.3k5 gold badges162 silver badges484 bronze badges
asked Feb 16, 2021 at 2:23
\$\endgroup\$
10
  • 3
    \$\begingroup\$ As you're new user, it's recommended that you leave the challenges in the Sandbox for Proposed Challenges - Code Golf Meta Stack Exchange for at least 72 hours before posting it on the main site \$\endgroup\$ Commented Feb 16, 2021 at 3:29
  • 9
    \$\begingroup\$ Is it allowed that a and b are identical? \$\endgroup\$ Commented Feb 16, 2021 at 3:29
  • 4
    \$\begingroup\$ Also is it allowed if a alone outputs source of b and a newline, and vice versa? \$\endgroup\$ Commented Feb 16, 2021 at 3:29
  • \$\begingroup\$ Are we allowed to ignore/redirect output from stderr when running our program? \$\endgroup\$ Commented Feb 16, 2021 at 8:02
  • \$\begingroup\$ stderr should have no output, unless all output is going through stderr. Basically, only either stdout or stderr should be used. \$\endgroup\$ Commented Feb 16, 2021 at 8:10

3 Answers 3

4
\$\begingroup\$

Bash, 63 37 bytes

a

t=;x(){ cat 2ドル 1ドル;};x b \

b

${t-cat} a

-26 bytes thanks to Nahuel Fouilleul

Try it online!

quick explanation

There are two key tricks:

  • We define our own version of cat called x which swaps its arguments, when there are 2 arguments given. It will have only 1 argument when a is run, but 2 arguments when c is run.
  • b uses bash default arguments to accomplish what it needs. When t is undefined, as it will be when b is run alone, it becomes cat a. When t is defined, it just returns a, becoming the second argument to a command x b a.
answered Feb 16, 2021 at 7:43
\$\endgroup\$
9
  • \$\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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented 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 cat is fine. \$\endgroup\$ Commented Feb 16, 2021 at 9:07
1
\$\begingroup\$

05AB1E, 39 (20+19) bytes

Program a:

"34çìD«r» ̈"34çìD«r» ̈

Try it online.

Program b:

"34çìD«r» ̈"34çìD«r»

Try it online.

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)
answered Feb 16, 2021 at 9:24
\$\endgroup\$
0
\$\begingroup\$

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..]}]"]
answered Nov 20, 2021 at 17:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.