3
\$\begingroup\$

A Ken Thompson virus is a virus that infects your compiler and other programs. Since it infects all your tools, you can not detect it without using tools from before the virus was introduced.

Your will create a prototype for inserting the Ken Thompson virus into code. The goal of this virus will be to allow you to (fake) destroy any computer running a program infected with it.

Inserting a Ken Thompson virus is defined as follows. Start with an input program P. Your program will output a modified program Q that works as follows:

  • If the input to Q is aNouIQ5xKvUfdZVzo0Xs, it will output BOOM (in a real Ken Thompson virus, this would actually do something bad, but since it this is a fake virus, outputing BOOM is done instead).
  • For any other input to Q, run program P with that input
    • If the output of P is valid source code in the language you are using, insert the Ken Thompson virus into it and then Q will output the resulting infected program.
    • Otherwise, the output of Q is just the output of P.

You will write a program that takes P's code as input and produces Q's code as output.

You can use either a currently (at the time of this post) existing programming language, or a subset of one, for this challenge. (The idea is that language you are using would be the target language of some self-compiling compiler, but this is not required.) Since you want to be as subtle as possible, the shortest code (in bytes) wins!

\$\endgroup\$
  • 3
    \$\begingroup\$ Voting to close as asking for malicous code is never a good idea. codegolf.meta.stackexchange.com/q/4829/15599 \$\endgroup\$ – Level River St Jun 4 '17 at 22:45
  • 1
    \$\begingroup\$ I'm voting to close this question as off-topic because I agree with @LevelRiverSt, this is malicious. \$\endgroup\$ – NoOneIsHere Jun 4 '17 at 22:54
  • 6
    \$\begingroup\$ @NoOneIsHere @LevelRiverSt This is clearly not malicious. Insert a simulated Ken Thompson virus into a program, The goal of this virus will be to allow you to (fake) destroy any computer. All the "virus" does is print BOOM. There is absolutely nothing malicious. \$\endgroup\$ – MD XF Jun 5 '17 at 3:22
  • 6
    \$\begingroup\$ I don't understand why this is downvoted. Seems like a quine variant that may actually be interesting for once. \$\endgroup\$ – feersum Jun 5 '17 at 8:23
  • 2
    \$\begingroup\$ @MDXF The OP's intention is not malicious, but in theory at least, code submitted here could be used to create a virus. The possibility of that happening is extremely remote, but if it did happen it would bring this site into serious disrepute. That's why I say: asking for malicious code is never a good idea. Per the question I linked, Martin, Peter and a majority of voters at the time tended to agree. \$\endgroup\$ – Level River St Jun 5 '17 at 20:27
2
\$\begingroup\$

For some other languages, determining If the output of P is valid source code in the language you are using is almost impossible, unless you use/rewrite a compiler. For some other languages you can eval a string and catch for error, if any. However, for Mathematica, it is just check for if the head of the result is a function - Mathematica doesn't allow output invalid function, like 1!-!&.

So,

Mathematica, 209 bytes

(obviously can be golfed more, for example by assign long names to global variables)

With[{p=#,c=With[{p=#,c=6},If[#==="aNouIQ5xKvUfdZVzo0Xs","BOOM",With[{r=p@#},If[Head@r===Head@p,(c/.3!->c)[r],r]]]&]&},If[#==="aNouIQ5xKvUfdZVzo0Xs","BOOM",With[{r=p@#},If[Head@r===Head@p,(c/.3!->c)[r],r]]]&]&

The function takes a function as input, and output a function according to the specification.

Note that the input must be a pure function, not something like f[x_] := .... So all Mathematica built-in functions fail the criteria.

Although Mathematica have #0 parameter, I explicitly disallow that in my answer because a quine is not allowed to read the code of itself.

(I thought that question is trivial until I read the third rule)

\$\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.