You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The upcoming Mindustry Logic 8 comes with a new format instruction. In short, the format instruction searches for placeholders in the form of {0} to {9} in the text buffer, and replaces the first occurrence of the lowest-numbered placeholder with the value of its argument. This mechanism provides numerous possibilities to dynamically build text output - see the link above for examples.
When compiling for target ML8A, the Print Merger optimization may use the format instruction to produce better code:
print "Minimum: {0}, middle: {0}, maximum: {0}\n"
format min
format mid
format max
printflush message1
Since the {0} placeholders are always replaced first, the optimization is safe as long as no {0} placeholders, apart from those that are used by the optimizer itself, appear in the text buffer. This this limitation is not so severe, as it leaves all the other placeholders, {1} to {9}, to be used by the code for dynamic formatting.
To provide maximum flexibility without the burden of having to manually disable this optimization in cases it could interfere with the rest of the code, Mindcode tries to detect the situations when the {0} sequence could appear in the text buffer. Since the only form of string manipulation in mlog are the print and format instructions themselves, there aren't that many ways to place the {0} placeholder into the text buffer. Some of them are:
#set target = ML8A;
print("{0}"); // Obvious
printflush(message1);
i = floor(rand(0.5)); // Produces 0 without Mindcode realizing it
print("{", i, "}");
printflush(message1);
print("{{1}}"); format("0");
printflush(message1);
Mindcode attempts to make sure the user's code may not produce {0} in the text buffer by inspecting all strings that appear in the compiled code for compromising patterns. These patterns are:
The {0} sequence anywhere in any string, obviously,
} at the beginning of any string or { at the end of any string,
The {{ or }} sequences in any of the strings.
It is, however, possible that there exists another possibility to smuggle {0} into the text buffer I've missed. So, this is where the challenge comes. Try to create a code which will place the {0} placeholder undetected by the above tests. This is a template for the test:
#set target = ML8A;
#set optimization = experimental;
// Place your sneaky code here
answer = rand(5) < 10; // Produces 1
println();
print($"The answer is $answer!");
printflush(message1);
You can compile and run this in Mindcode - and you'll see that the code prints The answer is 1!.
The goal is to insert some code at the indicated position to place {0} in the text buffer undetected by Mindcode. If you succeed and run the code, it will output something different from The answer is 1! on the last line - probably The answer is {0}!, but any other altercation of the output means you've succeeded.
If you do manage to break the optimization mechanism, I'll be very interested to hear about it. Good luck! :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The upcoming Mindustry Logic 8 comes with a new
formatinstruction. In short, theformatinstruction searches for placeholders in the form of{0}to{9}in the text buffer, and replaces the first occurrence of the lowest-numbered placeholder with the value of its argument. This mechanism provides numerous possibilities to dynamically build text output - see the link above for examples.When compiling for target ML8A, the Print Merger optimization may use the format instruction to produce better code:
compiles into
Since the
{0}placeholders are always replaced first, the optimization is safe as long as no{0}placeholders, apart from those that are used by the optimizer itself, appear in the text buffer. This this limitation is not so severe, as it leaves all the other placeholders,{1}to{9}, to be used by the code for dynamic formatting.To provide maximum flexibility without the burden of having to manually disable this optimization in cases it could interfere with the rest of the code, Mindcode tries to detect the situations when the
{0}sequence could appear in the text buffer. Since the only form of string manipulation in mlog are theprintandformatinstructions themselves, there aren't that many ways to place the{0}placeholder into the text buffer. Some of them are:Mindcode attempts to make sure the user's code may not produce
{0}in the text buffer by inspecting all strings that appear in the compiled code for compromising patterns. These patterns are:{0}sequence anywhere in any string, obviously,}at the beginning of any string or{at the end of any string,{{or}}sequences in any of the strings.It is, however, possible that there exists another possibility to smuggle
{0}into the text buffer I've missed. So, this is where the challenge comes. Try to create a code which will place the{0}placeholder undetected by the above tests. This is a template for the test:You can compile and run this in Mindcode - and you'll see that the code prints
The answer is 1!.The goal is to insert some code at the indicated position to place
{0}in the text buffer undetected by Mindcode. If you succeed and run the code, it will output something different fromThe answer is 1!on the last line - probablyThe answer is {0}!, but any other altercation of the output means you've succeeded.If you do manage to break the optimization mechanism, I'll be very interested to hear about it. Good luck! :)
Beta Was this translation helpful? Give feedback.
All reactions