(&<`q#
Try it online! (or try it here to avoid timing-out)
Outputs 2.
Explanation.
The goal is to output the ASCII character "2
" in a 7-byte or less program.
Malbolge code & data occupies the same memory space. When a Malbolge program is loaded, the last two single-byte instructions act as 'seeds' that determine how the remaining memory is initialized (in a deterministic but rather uncontrollable fashion). So if we try to construct 5 command/byte programs that use the contents of the memory to generate the ASCII encoding for "2
", we can try different combinations of the remaining 2 commands/bytes to try to find one that gives the correctly-initialized memory to achieve this. There are 8 valid Malbolge commands (and any other byte in the program will generate an error upon loading), so this gives us 8x8 combinations to try per 5 byte program. This is obviously less than the 1/256 chance of 'hitting' the ASCII character "2
" in any particular byte, so we'll probably have to try more than one program.
Each Malbolge command self-modifies immediately after execution, making re-use rather difficult. So here we try only single-pass programs, without any attempt to loop. We will need to use the commands j
(set the data pointer), <
(write an ASCII value to the output), and (probably) v
(end the program). This gives us room for 2 more data-altering commands in within the 5-byte limit, so we can try combinations of *
(rotate) and p
(tritwise OP operation), as well o
(no operation; but in Malbolge this changes the data pointer as a side effect so it can also affect the output).
Unfortunately, after trying all 64 combinations of the final two bytes across the Malbolge programs jpo<v..
, jop<v..
, ojp<v..
, jpp<v..
, jppp<v.
, jpppp<v
, j*o<v..
, j*p<v..
, j*pp<v.
and j*ppp<v
(where .
represents any of the 8 Malbolge commands), we can only generate the output numbers 0, 1, 3, 5, 6, 7, 8, 9. At this point it seems likely that Kamila Szewczyk may have used a similar approach, and therefore hoped that generating the character "2
" in 7 bytes or less is impossible...
But: what about shorter programs that omit the v
(end the program) command? These allow the memory to be initialized differently, and so we can maybe find a combination that enables output of "2
"... but the Malbolge interpreter will now continue reading bytes from the rest of the initialized memory and executing them as commands, with rather uncontrollable consequences! Still, if it hits a v
(end of program) before it hits a <
(write output), that could be Ok: so let's try it!
After some searching, we find that j*p<..
is indeed able to initialize the memory to output "2
", using two different suffix combinations: when test-run, <j
unfortunately keeps running and outputs an additional "2L
" before stopping, but /j
stops after the "2
". It's a crack!
To load into Malbolge, the final program - j*p</j
- must finally be encoded using a series of operations (see the spec) to yield the final loadable code of (&<`q#
.