mbomb007 asked us to make a self mutilating program. Its an interesting challenge, but I personally don't like to encourage my programs to be self-mutilating. I like to think that, at the heart of every program is a beautiful butterfly waiting to break out.
As such, for my first ever Code Golf challenge, I challenge Stack Exchange to metamorph a butterfly. Your program should accept its own code as input and output
I am a caterpillar! followed by the code for another program in the same language (you may put a divider between the outputs or not... but we're talking butterflies, so prettiness counts!). This second program should output
I am a beautiful butterfly!
Metamorphosis: This program should modify itself, which is why it is being passed itself as input. I don't want a boring nested program that is nothing but a print statement
I am a caterpillar! followed by printing source code. You have to metamorph the code itself, so the most boring acceptable answer is one which prints
I am a caterpillar, then rips a string out of the input (which happens to be code) and prints that. Of course, this is a very tricky little requirement to write, so it should be thought of as a "spirit of the challenge." The formal requirement is going to be set at "If your program is given a random string of the same length as your program, there should be less than a 5% chance of printing
I am a butterfly when the second program is run."
- Write a program which, when provided its own code as input, prints
I am a caterpillar!followed by a block of source code (with a delimiter between them or not).
- This second block of source code, when compiled/interpreted as the same language, prints
I am a beautiful butterfly!
- If your program is not fed its own source code as input, and is instead given a random string of characters that does not match its own code, the result must either fail to compile/interpret, or not print
I am a butterfly!for at least 95% of random strings (spirit: you are expected to read your own source code from the input, and stitch it together into a butterfly).
- I'm comfortable with you not proving this, so long as you're holding to the spirit of the metamorphosis idea, modifying your own code, but if you're trying to play loophole games to work around using your inputs, expect to have to prove it.
- Standard loopholes apply
I'd like these to be judged by their beauty, but that's a decidedly non-quantitative approach, so the usual "number of bytes" route can be used to judge
Bonus: -20% - pick any 3 letter string that doesn't match the first 3 letters of your program. If I run the caterpillar program, but modify the input so that the first 3 character are changed to the specified ones, print
I am Batman! instead. Always be Batman. (Same random string rules apply for this... metamorphosis!)
Trivial example (python):
""" print "I am a beautiful butterfly!" #print "I am Batman!" """ import sys code = sys.stdin.read() if code.startswith("YYZ"): # swap the comments between the print statements code = code.replace("#print", "_a").replace("print", "#print").replace("_a", "print") code = [3:] # remove """ or XYZ code = code.split('"""') print "I am a caterpillar!" print code