One thing that we do not know is how James T. Kirk in the original series beat the no-win scenario: it is specifically left unmentioned, only famous enough that McCoy was able to point out that Kirk was the only one to ever beat it. So if even a medical officer knew the tale — which he ought to by then, after completing a five year mission and returning to San Fransisco to continue serving as an officer at the Academy — then it must have been pretty hard to cheat. And most likely that loophole was closed.
I figure there are two parts: first is that Kirk took the test a fourth time most likely just after failing* a third time, he probably volunteered to help restore the simulator after “destroying” it spectacularly. That’s how he managed to get the scenario reconfigured, most likely with the help of his late buddy Gary Mitchell. The second part is that it was probably a little thing like where the Klingons watching the Neutral Zone were when entering, or placing a minor navigational hazard in their way to make them detour just enough to save at least some of the Maru’s crew and passengers. Make it look like a close thing. Use clever positioning of the ship in three dimensions to eke out another second here and there. Make the test drag on, but not tip the hand so that the evaluators stop to look for cheating.
*It is important to note that the real test was not surviving, but how the commander keeps his bridge crew together in a stressful situation, when the senior officers are incapacitated and the ship doomed. How does the command candidate react? Is the candidate mature enough for the center seat? Milgram applied to command competence evaluation, really.