Coding Games and Programming Challenges to Code Better
Send your feedback or ask for help here!
I am trying to solve this puzzle (in PHP):
In case I misunderstood the task:
My code works but it is too slow for the last test. I have already tried a lot of tweaks, but it still fails. Looks like I am really missing out some âshortcutââŚ
This is the part of the code that does most of the work:
function play ($number, $N, $level) { ++$level; $N -= $number; if ($N < 0 || in_array($N, NEXT_MOVES[$number])) { return false; } if ($N == 0 || min(NEXT_MOVES[$number]) > $N) { return true; } foreach (NEXT_MOVES[$number] as $nextMove) { if ($nextMove > $N) continue; if (play($nextMove, $N, $level)) return false; } return true; }
I play the numbers in descreasing order (highest possible first). As soon as one side âwinsâ, I go back one âlevelâ and try the next number. The $level variable is there for debugging purposes only.
I changed it all up and replaced the function for 2 additional loops. that âworkedâ, but itâs not as pretty as this solution was but I learned something new about how to really increase the speed
I donât understand what makes a winning starting moveâŚI can always find a path to a win based on the starting number and subsequent moves therefore I must not understand something. For instance in test case 2, N = 9 and the winning numbers are 1,7,8,9.
However if I pick 2, N = 7 and player two has options 1, 3, 4, 5 or 6.
âif he picks 6 I lose because N = 1 and my options are 2,3,5,8,9 all would make N negative
âif he picks 5 I win because N = 2 and my only non negative options are 1 or 2 which make him lose
âif he picks 4 I lose
âif he picks 3 I lose
âif he picks 1 we go another round with N =6 and my options are 2, 3, 5 (8,9) are losers
----I pick 5 I lose because N = 1 and he picks 1
----I pick 3 I win
----I pick 2 we go another round with 2 more outcomes 1 win each.
Based on the above I would conclude that 2 is a winner when starting at 9 can someone explain what I am missing?
In game theory we say something is a âwinning moveâ for player 1 if for all subsequent moves by player 2, player 1 will still always win.
Therefore for N = 9, 2 is not a winning move. You said âIf [player 2] picks 6 I loseâ. Therefore the initial move of 2 does not guarantee a win for player 1.
However, for N = 9, 1 is an example of a winning move. Here player 2 has 3 options which are 4, 5 or 2.
1 > 4 > 1 > 2 > 1 Player 1 wins
1 > 5 > 3 Player 1 wins
1 > 2 > 6 Player 1 wins
Notice how there are no choices player 2 can make which makes player 1 lose.
Thank you. That makes this puzzle much easier to understand.
For the value N = 35
if the first player starts by choosing 1
The best selection for the first player to win is 1 2 3 5 7 8 9 (at the end N = 0)
While the second player can also win if the choice is following this sequence 1 4 7 8 9 6 (at the end N = 0)
So the best sequence is the second one (1 4 7 8 9 6). As a result, the second player wins
What is the criterion for choosing the best sequence?
It wouldnât hurt if this definition of a winning move would be included in the statement of this puzzle.
Iâm trying to solve this and I pass all of the test cases, but I fail validator 1, can you give me any hints regarding to what validator 1 is testing for so that I can fix the issue?
Validator 1: N is a single-digit positive integer. You can go through each possible value one by one to see where things might have gone wrong.
I did that though and when I analyzed them by hand they all appeared correct:
[[1], [1, 2], [3], [1, 3, 4], [5], [3, 6], [1, 3, 4, 6, 7], [7, 8], [1, 7, 8, 9]]
I know that 2 and 9 are right, I manually checked the rest and they all looked right to me, did I miss something?
Looks correct to me, so probably the correct result is simply not output correctly? For example, could it be that the output contains some extra spaces or newlines?
It shouldnât, I used the built in tools in python to format it .join
, sorted
, etc⌠unless the validator maybe has extra spaces or something?
oh, I figured it out⌠I had a base case for â1â, but didnât break out of the execution so it printed it twice lol