I'm doing some game simulation of the game shut the box for 538's weekly riddle. I'm constructing simulations of the game using a list of all possible moves, which I achieved using this code:
CleanList[list_] := DeleteDuplicates[list];
RemoveLists[list_, x_] := Select[list, Total@# == x &]
Moves[x_] := RemoveLists[CleanList /@ IntegerPartitions[x], x]
AllPossibleMoves = {}; For[i = 1, i <= 9, i++, AppendTo[AllPossibleMoves, Moves[i]]];
Which gives us the list:
AllPossibleMoves = {{{1}},{{2}},{{3},{2,1}},{{4},{3,1}},{{5},{4,1},{3,2}},{{6},{5,1},{4,2},{3,2,1}},{{7},{6,1},{5,2},{4,3},{4,2,1}},{{8},{7,1},{6,2},{5,3},{5,2,1},{4,3,1}},{{9},{8,1},{7,2},{6,3},{6,2,1},{5,4},{5,3,1},{4,3,2}}}
From there, I took the initial game setup (InitialGameState=Range[9]
) and used the code I got from this wonderful site yesterday to compute all the possible states after the first move:
MakeMove[list1_,list2_]:=Complement[Union[list1,list2],Intersection[list1,list2]];
For[i=1,i<=Length[AllPossibleMoves],i++, AppendTo[Turn2States, For[j=1,j<=Length[AllPossibleMoves[[i]]],j++,AppendTo[Turn2States,MakeMove[InitialGameState,AllPossibleMoves[[i,j]]]]]]]
Then I deleted the Nulls, to clean up the list (Turn2States = DeleteCases[Turn2States, Null]
).
From here though, I don't know how to proceed. I was going to use MakeMove
with AllPossibleMoves
and Turn2States
. Unfortunately, using MakeMove
like MakeMove[{1,2,3},{2,3,4,5,6,7,8,9}]
gives {1, 4, 5, 6, 7, 8, 9}
. Which just gets me going in circles because it adds the 1 back in. Is there a simple way to delete a list of numbers from another list like so:
Operation[{1,3,4,5,7},{1,2,3,4,5,6}] = {2,6}
Edit: I found this code on a similar question but it doesn't seem to work for me. I tried doing
list = DeleteCases[{1, 2, 3, 4, 5, 6}, Alternatives[{1, 2, 4, 7, 8, 9}]];
But that gives: list = {1, 2, 3, 4, 5, 6}
instead of list = {3}
Operation[a_,b_]:=Complement[b,a]
? $\endgroup$Alternatives
should beAlternatives @@ {1, 2, 4, 7, 8, 9}
or simplyAlternatives[1, 2, 4, 7, 8, 9]
. $\endgroup$