24
\$\begingroup\$

Write two programs such that when the bytes of their source code are XOR-d together it produces a third, valid program, all in the same language. The length of each of the three programs in bytes must be identical.

The first program should output A. The second program should output B. The third program should output C.

A newline is optional, but if one outputs a newline, both of the others must too.

If you use any command line flags you must you the same ones for each program, i.e. all three programs must be executed the same way. If you're using a language like PHP that traditionally starts with the same combination of characters, e.g. <? but there is a command line option that drops those characters you may use that instead, i.e. PHP must only be run from the command line.

No input is allowed. Each program must be self-contained.

If your program is written entirely in ASCII the following tables might help:

Codes 0x20 - 0x3F
 !"#$%&'()*+,-./
0123456789:;<=>?

Codes 0x40 - 0x5F
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_

Codes 0x60 - 0x7E
`abcdefghijklmno
pqrstuvwxyz{|}~

EDIT

Using non-printing characters is allowed as I didn't expressly forbid it originally. However, if you want to post a version that uses only printable characters as well that would be great! (Score whichever is the shortest.) The "clever factor" of using control codes has somewhat worn off now...

END EDIT

XOR-ing any two codes from the same block will produce a code less than 0x20 which is therefore not a valid printable ASCII code, e.g. 0x41 (A) XOR 0x5A (Z) = 0x1B. XOR-ing any two codes from different blocks will produce a code from the third block, e.g. 0x61 (a) XOR 0x54 (T) = 0x35 (5).

    |  @` !Aa "Bb #Cc $Dd %Ee &Ff 'Gg (Hh )Ii *Jj +Kk ,Ll -Mm .Nn /Oo 0Pp 1Qq 2Rr 3Ss 4Tt 5Uu 6Vv 7Ww 8Xx 9Yy :Zz ;[{ <\| =]} >^~ ?_
--- + --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 @` |  @` !Aa "Bb #Cc $Dd %Ee &Ff 'Gg (Hh )Ii *Jj +Kk ,Ll -Mm .Nn /Oo 0Pp 1Qq 2Rr 3Ss 4Tt 5Uu 6Vv 7Ww 8Xx 9Yy :Zz ;[{ <\| =]} >^~ ?_
!Aa | !Aa  @` #Cc "Bb %Ee $Dd 'Gg &Ff )Ii (Hh +Kk *J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Gg &Ff !Aa  @` #Cc "Bb
>^~ | >^~ ?_ <\| =]} :Zz ;[{ 8Xx 9Yy 6Vv 7Ww 4Tt 5Uu 2Rr 3Ss 0Pp 1Qq .Nn /Oo ,Ll -Mm *Jj +Kk (Hh )Ii &Ff 'Gg $Dd %Ee "Bb #Cc  @` !Aa
?_ | ?_ >^~ =]} <\| ;[{ :Zz 9Yy 8Xx 7Ww 6Vv 5Uu 4Tt 3Ss 2Rr 1Qq 0Pp /Oo .Nn -Mm ,Ll +Kk *Jj )Ii (Hh 'Gg &Ff %Ee $Dd #Cc "Bb !Aa  @`

Look up two characters from different columns and pick the character from their intersection that's in the other column, e.g. B and e: "Bb XOR %Ee is 'Gg. B is second column and e is third column so pick the character from the first column in 'Gg: '.

Example source:

 Ld+|?dpHs'-,pUe    (Program 1)
~bWn=n5Xe!t_G($3    (Program 2)
^.3EAQQ(-RSrkXqV    (Program 3)

Note that in each column there can only be one digit, one uppercase letter and one lowercase letter. For each column you may use any character you like in the first program, then your choice of 64 characters in the second program but only one possible character in the third program. 0x7F is not valid anywhere.

If you use Unicode it's up to you to validate the XOR-ing and you must say how it's encoded, e.g. UTF-8, UTF-16, etc. Each file must use the same encoding.

Score: Length of one program. Fewest bytes win!

\$\endgroup\$
13
\$\begingroup\$

GolfScript, 6 bytes

A

A'AA'|

B

cdc"B"

C

"C"ce^

| and ^ are setwise or and xor with the empty input string, which would do nothing but remove duplicates in the string.

Original version (with non-printable characters)

A

Xz'A'v

B

z9\x05"B"

C

"C"ceT

GolfScript ignores the undefined things, and the three programs are just 'A' "B" "C".

\$\endgroup\$
12
\$\begingroup\$

Javascript, 23 bytes

Program A:

alert(' A '[~-~~2],'B')

Program B:

alert(FL$RSsYnYW1f='B')

Program C:

\0\0\0\0\0\0alert('C')\0;\0\0\03\0\0

You can test the conversion with the following function:

function xorString(a, b) {
  b = b || '';
  var length = Math.max(a.length, b.length);
  var chars = a.split('').map(function(charA, i) {
    var charB = b[i] || ' ';
    return String.fromCharCode(charA.charCodeAt(0) ^ charB.charCodeAt(0));
  });

  return chars.join('');
}
\$\endgroup\$
  • \$\begingroup\$ Nice one!!! You can include a stacksnippet showing this. \$\endgroup\$ – Ismael Miguel May 30 '15 at 15:40
3
\$\begingroup\$

PHP, 15+1 bytes

This one was easy REALLY harder than expected, but fun!
Sadly, this is the 3rd rewrite due to invalid answers.

This time, it is meant to run in the console (using the parameter -r).

Program A:

echo A;E925Z9A;

Program B:

echo B; zzzzzz;

Result of XORing:

\0\0\0\0\0\3\0eCHO C;\0 //base 64: AAAAAAADAGVDSE8gQzsA

Hex dump:

   |00|01|02|03|04|05|06|07|08|09|0A|0B|0C|0D|0E|0F|      TEXT      
--------------------------------------------------------------------
00 |00|00|00|00|00|03|00|65|43|48|4F|20|43|3B|00|  |.......eCHO C;.
====================================================================

PHP will throw warnings about having the \0 and \3 chars before the echo, but that is ignorable.
It's just warnings!
It still runs and outputs the right answer!


Try running this, in your console:

php -r "@eval('echo B; zzzzzz;' ^ 'echo A;E925Z9A;');"

If you can't, try one of these:

If you still cant, run the code yourself:

<?php

error_reporting( 0 );

$program_a = 'echo A;E925Z9A;';

$program_b = 'echo B; zzzzzz;';

eval( $program_a );

eval( $program_b );

eval( $program_c = $program_a ^ $program_b );

echo PHP_EOL, 'a ^ b = ';

var_dump( $program_c );

Old code, invalid:

Program A:

<?=A;zzzzz;

Program B:

<?=B;FEG9A;

Result of XORing:

\0\0\0\0<?=C;\0 //base64: AAAAAwA8Pz1DOwA=
\$\endgroup\$
  • \$\begingroup\$ I can't actually type \x00 into my console but replacing them with \x03 works and \x00 within PHP itself on my server still runs so I'm assuming your answer is valid now! I also had to use a flag to suppress all errors: php -d error_reporting=0 -r "^C^C^C^C^C^CeCHO C;^C" Outputs C. Redirecting stderr (2> under Windows) didn't suppress all errors from the output. \$\endgroup\$ – CJ Dennis May 30 '15 at 14:58
  • \$\begingroup\$ Your program in the Windows console: " ♥ eCHO C; "! \$\endgroup\$ – CJ Dennis May 30 '15 at 15:03
  • \$\begingroup\$ @CJDennis Warnings are always disregarded. If it was a fatal error, it would be worst. But I find it weird that it outputs that, when XORing. But maybe that is because of the \3 there, that shows as the hearts club. But do you want me to add the -d flag to the count? \$\endgroup\$ – Ismael Miguel May 30 '15 at 15:06
  • \$\begingroup\$ Since you're not beating the GolfScript code, don't worry about it. If you find a 6 byte version then I'll have to reconsider! But I had to work really hard without reconfiguring my php.ini file to get it to work cleanly! I liked the way Windows turned the \x03 into a heart! Windows ♥ PHP! \$\endgroup\$ – CJ Dennis May 30 '15 at 15:11
  • 1
    \$\begingroup\$ Yes, the @ managed to suppress all errors! \$\endgroup\$ – CJ Dennis May 30 '15 at 15:42
3
\$\begingroup\$

Pip, 4 bytes

Since user23013 is using non-printing ASCII in GolfScript, I figured I could do the same in Pip. It does require redirecting stderr, but that's okay, right?

A

??'A

where the two ?s represent ASCII character 0.

B

'C'B

C

'C??

where the two ?s represent ASCII characters 0 and 3, respectively.

Place each program in a file (A.pip for example). On Linux, do:

./pip.py A.pip 2>/dev/null

On Windows:

pip.py A.pip 2>nul

If error output is verboten, here's a 5-byte solution in printable ASCII:

pAa'A
 f"B"
P'Cec
\$\endgroup\$
  • \$\begingroup\$ Yes, both redirecting stderr (ugly as it is!) and using non-printing characters are allowed. I appreciate you posting a completely printable version too, though! \$\endgroup\$ – CJ Dennis May 31 '15 at 0:56
2
\$\begingroup\$

Microscript, 8 bytes each

A: 'APhgap)

B: 'BPh@" A

C: <0><3><0><0>'CPh

\$\endgroup\$
  • \$\begingroup\$ I didn't realise so many languages are very forgiving with control codes in the source. Take a look at the other answers, some of which do use non-printing codes. Feel free to do the same! \$\endgroup\$ – CJ Dennis May 31 '15 at 0:53
  • \$\begingroup\$ I thought it said not to use non-printing ASCII, but I may be wrong. EDIT: And now I see that you've changed it. Alright, I'll see what I can do. \$\endgroup\$ – SuperJedi224 May 31 '15 at 2:49
  • 1
    \$\begingroup\$ Shouldn't that be <0><3><0><0>...? \$\endgroup\$ – CJ Dennis May 31 '15 at 7:43
  • \$\begingroup\$ You're right. Thank you for pointing that out. \$\endgroup\$ – SuperJedi224 May 31 '15 at 11:16
2
\$\begingroup\$

Python, 19 15 14 bytes

a.py

00000000  70 72 69 6e 00 28 32 50  69 0a 74 22 41 22        |prin.(2Pi.t"A"|

b.py

00000000  70 72 69 6e 74 22 42 22  00 64 00 00 02 00        |print"B".d....|

c.py

00000000  00 00 00 00 74 0a 70 72  69 6e 74 22 43 22        |....t.print"C"|

I came up with this solution when I noticed that python (2.7.6 on Ubuntu 14.04) after seeing a NUL character will ignore the rest of that line.

Shortened by 1 bytes once I realized that the space after print wasn't needed. And shortened by 4 more bytes once I realized that the NUL character also cause the newline to be ignored so I can use it to break lines in the middle of a keyword.

\$\endgroup\$
0
\$\begingroup\$

Chip, 8 bytes

All require the flag -w.

Program A

a*gt*...

(. is the null character \x00)

Program B

...*gb*t

(. is the null character \x00)

Program C

a*g^Mb*t

Both A and B produce three warnings each (e.g.: 1:6 WARN: '' (0) is not a valid character), but they can be safely ignored since they go to stderr. The elements ^ and M are valid, but do nothing here.

I'm fairly sure that this is the best that can be done with non-printables allowed. I'm not sure this proof is complete, but here goes:

A requires a, g, t, and some way to activate those elements (a minimum of two symbols are needed for this, and * is the simplest option here). Similarly,
B requires b, g, t, and two *s, and
C requires a, b, g, t, and two *s.

If C borrows its symbols directly from A and B, there will be two letters and two *s that remain unclaimed. (In my implementation, A's t and B's g). Those four elements can be overlapped so that they are mangled in C (producing ^ and M in this case). We now have those two mangled elements, plus the six required elements from above (C requires 6, the others only 5, but we need max() here), for a total of 8 elements.


Chip (warning-free), 10 bytes

All require the flags -wo.

Program A

a*HH^t*Gg 

(there is a trailing space)

Program B

KKb*t*^g G

Program C

*a*b*^t Gg

This is provably the best we can do within printable ASCII, as follows: The three programs, combined, need a total of 2 as, 2 bs, 3 gs, and 3 ts. Since none of these characters can transition to another, we need 2+2+3+3 = 10 separate positions.

\$\endgroup\$

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.