(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) \!\(\* RowBox[{"sudoku", "=", GridBox[{ {"0", "0", "0", "0", "0", "1", "0", "8", "0"}, {"2", "0", "5", "6", "0", "0", "0", "0", "4"}, {"0", "8", "0", "7", "0", "0", "0", "3", "0"}, {"0", "5", "1", "0", "0", "0", "0", "9", "0"}, {"0", "0", "0", "0", "9", "2", "0", "0", "8"}, {"0", "0", "2", "5", "0", "0", "7", "0", "0"}, {"4", "3", "0", "0", "1", "0", "0", "0", "6"}, {"0", "0", "7", "9", "6", "0", "0", "4", "3"}, {"0", "0", "0", "0", "0", "0", "0", "0", "0"} }]}]\) \!\(\* RowBox[{"sudokuerror", "=", GridBox[{ {"0", "0", "0", "0", "0", "1", "0", "8", "0"}, {"2", "0", "5", "6", "0", "0", "0", "0", "4"}, {"0", "8", "0", "7", "0", "0", "0", "3", "0"}, {"0", "5", "1", "0", "0", "0", "0", "9", "0"}, {"0", "0", "0", "0", "9", "2", "1", "0", "8"}, {"0", "0", "2", "5", "0", "0", "7", "0", "0"}, {"4", "3", "0", "0", "1", "0", "0", "0", "6"}, {"0", "0", "7", "9", "6", "0", "0", "4", "3"}, {"0", "0", "0", "0", "0", "0", "0", "0", "0"} }]}]\) \!\(\* RowBox[{"sudokuillegal", "=", GridBox[{ {"0", "0", "0", "0", "1", "1", "0", "8", "0"}, {"2", "0", "5", "6", "0", "0", "0", "0", "4"}, {"0", "8", "0", "7", "0", "0", "0", "3", "0"}, {"0", "5", "1", "0", "0", "0", "0", "9", "0"}, {"0", "0", "0", "0", "9", "2", "1", "0", "8"}, {"0", "0", "2", "5", "0", "0", "7", "0", "0"}, {"4", "3", "0", "0", "1", "0", "0", "0", "6"}, {"0", "0", "7", "9", "6", "0", "0", "4", "3"}, {"0", "0", "0", "0", "0", "0", "0", "0", "0"} }]}]\) \!\(\* RowBox[{"sudoku3a", "=", GridBox[{ {"0", "0", "0", "0", "5", "4", "9", "0", "0"}, {"0", "0", "8", "2", "0", "0", "0", "7", "0"}, {"3", "0", "5", "0", "0", "0", "0", "0", "2"}, {"0", "0", "1", "0", "0", "3", "6", "8", "0"}, {"7", "0", "0", "0", "0", "0", "0", "0", "0"}, {"0", "0", "9", "0", "6", "1", "0", "0", "4"}, {"0", "0", "3", "0", "9", "8", "0", "4", "0"}, {"0", "0", "0", "7", "0", "0", "0", "0", "6"}, {"0", "5", "2", "0", "0", "0", "0", "1", "0"} }, GridFrame->True, RowLines->True, ColumnLines->True]}]\) \!\(\* RowBox[{"sudokujune232005", "=", GridBox[{ {"0", "0", "6", "0", "0", "5", "7", "0", "3"}, {"0", "0", "0", "4", "0", "0", "0", "8", "0"}, {"0", "0", "0", "9", "3", "0", "0", "0", "0"}, {"0", "4", "0", "0", "0", "0", "2", "0", "1"}, {"0", "0", "1", "0", "0", "0", "3", "0", "0"}, {"6", "0", "5", "0", "0", "0", "0", "4", "0"}, {"0", "0", "0", "0", "1", "4", "0", "0", "0"}, {"0", "1", "0", "0", "0", "9", "0", "0", "0"}, {"2", "0", "4", "3", "0", "0", "6", "0", "0"} }, GridFrame->True, RowLines->True, ColumnLines->True]}]\) \!\(\* RowBox[{"sudokujune242005hard", "=", GridBox[{ {"8", "0", "1", "9", "0", "0", "0", "0", "5"}, {"0", "7", "0", "0", "0", "0", "0", "6", "0"}, {"0", "0", "0", "5", "6", "0", "2", "0", "0"}, {"0", "5", "0", "0", "0", "0", "0", "0", "6"}, {"0", "0", "0", "4", "3", "8", "0", "0", "0"}, {"3", "0", "0", "0", "0", "0", "0", "9", "0"}, {"0", "0", "9", "0", "7", "5", "0", "0", "0"}, {"0", "2", "0", "0", "0", "0", "0", "7", "0"}, {"5", "0", "0", "0", "0", "9", "4", "0", "1"} }, GridFrame->True, RowLines->True, ColumnLines->True]}]\) \!\(\* RowBox[{"sudokuadvanced", "=", GridBox[{ {"1", "0", "0", "0", "2", "0", "0", "0", "3"}, {"9", "0", "0", "1", "0", "0", "0", "0", "8"}, {"6", "0", "0", "0", "0", "4", "0", "0", "2"}, {"4", "3", "0", "7", "0", "0", "0", "0", "5"}, {"7", "0", "2", "0", "6", "0", "4", "0", "1"}, {"8", "0", "0", "4", "0", "1", "0", "9", "0"}, {"2", "0", "0", "9", "0", "0", "0", "0", "0"}, {"3", "0", "0", "0", "0", "8", "0", "0", "4"}, {"5", "0", "0", "0", "7", "0", "0", "0", "9"} }, GridFrame->True, RowLines->True, ColumnLines->True]}]\) subgridanchors={{1,1},{1,4},{1,7},{4,1},{4,4},{4,7},{7,1},{7,4},{7,7}} extract3columns[m_,range_]:=Map[Take[#,range]&,m] gridcols[c_]:= Switch[c, 1,{1,3}, 2,{1,3}, 3,{1,3}, 4,{4,6}, 5,{4,6}, 6,{4,6}, 7,{7,9}, 8,{7,9}, 9,{7,9} ] subgrid[m_,r_,c_]:= Transpose[ extract3columns[ Transpose[ extract3columns[ m,gridcols[c] ] ],gridcols[r] ] ] illegalvalues[m_,r_,c_]:= Intersection[ Union[ Intersection[ Union[ m[[r]] ],{1,2,3,4,5,6,7,8,9} ], Intersection[ Union[ (Transpose[m])[[c]] ],{1,2,3,4,5,6,7,8,9} ], Flatten[subgrid[m,r,c]] ], {1,2,3,4,5,6,7,8,9} ] legalvalues[m_,r_,c_]:= If[m[[r,c]]\[Equal]0,Complement[{1,2,3,4,5,6,7,8,9}, illegalvalues[m,r,c] ],{} ] subgridlegalvalues[m_,r_,c_]:= Module[{legalvaluestable}, legalvaluestable=Table[legalvalues[m,r1,c1],{r1,9},{c1,9}]; Transpose[ extract3columns[ Transpose[ extract3columns[ legalvaluestable,gridcols[c] ] ],gridcols[r] ] ] ] rowsCheckTable[m_]:= Table[ Table[ Count[m[[r]],n] ,{r,9} ] ,{n,9} ] colsCheckTable[m_]:= rowsCheckTable[Transpose[m]] gridsCheckTable[m_]:= Table[ Table[ Count[Flatten[subgrid[m,subgridanchors[[g,1]],subgridanchors[[g,2]]]], n] ,{g,9} ] ,{n,9} ] checkSudokuLegal[m_]:= Length[Intersection[ Union[Flatten[rowsCheckTable[m]]],{2,3,4,5,6,7,8,9}]]\[Equal]0 && Length[ Intersection[ Union[Flatten[colsCheckTable[m]]],{2,3,4,5,6,7,8,9}]]\[Equal]0 && Length[ Intersection[ Union[Flatten[gridsCheckTable[m]]],{2,3,4,5,6,7,8,9}]]\[Equal]0 singleCellsTable[m_]:= Table[ If[ m[[i,j]]\[Equal]0,Length[legalvalues[m,i,j]],x ] ,{i,1,9},{j,1,9} ] singleCellsAnalysis[m_,count_]:= Position[ singleCellsTable[m] ,count] singleRowsTable[m_]:= Table[ Table[ If[Count[m[[r]],n]\[Equal]0, Apply[Plus, Table[Count[legalvalues[m,r,c],n] ,{c,9}]] ,x ],{r,9} ] ,{n,9} ] singleRowsAnalysis[m_,count_]:= Position[ singleRowsTable[m] ,count] singleColsTable[m_]:= singleRowsTable[Transpose[m]] singleColsAnalysis[m_,count_]:= singleRowsAnalysis[Transpose[m],count] singleGridsTable[m_]:= Table[ Table[ If[Count[ Flatten[subgrid[m,subgridanchors[[g,1]],subgridanchors[[g,2]]]], n]\[Equal]0, Count[ Flatten[subgridlegalvalues[m,subgridanchors[[g,1]], subgridanchors[[g,2]]]],n] ,x ],{g,9} ] ,{n,9} ] singleGridsAnalysis[m_,count_]:= Position[ singleGridsTable[m] ,count] singleCellsAnalysisCount1[m_]:= Map[Append[#,First[legalvalues[m,#[[1]],#[[2]]]]]&, singleCellsAnalysis[m,1] ] singleRowsAnalysisCount1[m_]:= Map[ Part[Append[#, Position[Table[legalvalues[m,#[[2]],c1],{c1,9}], #[[1]]][[1,1]]],{2,3,1}]&, singleRowsAnalysis[m,1] ] singleColsAnalysisCount1[m_]:= Map[Part[#,{2,1,3}]&, singleRowsAnalysisCount1[Transpose[m]] ] singleGridsAnalysisCount1[m_]:= Module[{tmp}, tmp=Map[ Part[Append[#, Take[Position[ subgridlegalvalues[m,subgridanchors[[#[[2]],1]], subgridanchors[[#[[2]],2]]],#[[1]]] [[1]],2] ],{2,3,1}]&, singleGridsAnalysis[m,1] ]; Map[Append[(subgridanchors[[#[[1]]]]+#[[2]]-{1,1}),#[[3]]]&,tmp] ] checkSudokuZeroOptions[m_]:= Length[singleCellsAnalysis[m,0]]\[Equal]0 && Length[singleRowsAnalysis[m,0]]\[Equal]0 && Length[singleColsAnalysis[m,0]]\[Equal]0 && Length[singleGridsAnalysis[m,0]]\[Equal]0 singleRowsTableIdenticalOptions[m_]:= Table[ Table[ If[Count[m[[r]],n]\[Equal]0, Count[Table[legalvalues[m,r,c],{c,9}],{n}] ,x ],{r,9} ] ,{n,9} ] singleRowsAnalysisIdenticalOptions[m_,count_]:= Position[ singleRowsTableIdenticalOptions[m] ,count] singleColsTableIdenticalOptions[m_]:= singleRowsTableIdenticalOptions[Transpose[m]] singleColsAnalysisIdenticalOptions[m_,count_]:= Position[ singleColsTableIdenticalOptions[m] ,count] singleGridsTableIdenticalOptions[m_]:= Table[ Table[ If[Count[ Flatten[subgrid[m,subgridanchors[[g,1]],subgridanchors[[g,2]]]], n]\[Equal]0, Count[ Flatten[subgridlegalvalues[m,subgridanchors[[g,1]], subgridanchors[[g,2]]],1],{n}] ,x ],{g,9} ] ,{n,9} ] singleGridsAnalysisIdenticalOptions[m_,count_]:= Position[ singleGridsTableIdenticalOptions[m] ,count] checkSudokuTwoIdenticalOptions[m_]:= Length[singleRowsAnalysisIdenticalOptions[m,2]]\[Equal]0 && Length[singleColsAnalysisIdenticalOptions[m,2]]\[Equal]0 && Length[singleGridsAnalysisIdenticalOptions[m,2]]\[Equal]0 solveSudokoBasic[m_]:= Module[{tmp}, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoBasic[ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoBasic[ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoBasic[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoBasic[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], m ] ] ] ] ] solveSudokoBasicBifurcated[m_]:= Module[{tmp}, If[!MemberQ[Flatten[m],0], Print["Found! "];Print[MatrixForm[m]];m, If[!checkSudokuLegal[m], Print["Shouldn't happen - illegal matrix found!"]; Print[MatrixForm[m]];m, If[!checkSudokuZeroOptions[m], Print["No way to put a number in one of the structures!"]; Print[MatrixForm[m]];m, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoBasicBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoBasicBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoBasicBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoBasicBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[(tmp=bifurcation[m])[[1]]===2, Print["Found! "];Print[MatrixForm[tmp[[2]]]];tmp[[2]], If[Length[(tmp[[2]])]>0, Print["Bifurcation ",tmp]; solveSudokoBasicBifurcated[ ReplacePart[m, tmp[[2]][[1,3]],{tmp[[2]][[1,1]], tmp[[2]][[1,2]]}]], m ] ] ] ] ] ] ] ] ] ] bifurcation[m_]:= Module[{tmp=singleCellsAnalysis[m,2],solution,tmplist}, (* singleCellsAnalysis[m,2] lists the positions of non- empty cells cells with exactly two legal values *) If[Length[tmp]>0, tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["Checking Bifurcation with ",tmplist]; If[MemberQ[ Flatten[(solution= solveSudokoBasicBifurcated[ ReplacePart[m, legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]],{tmp[[1,1]], tmp[[1,2]]}]])],0], (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["alternative guess chosen ",tmplist]; List[1,tmplist]), (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess chosen ",tmplist]; List[2,solution]) ],{1,{}} ] ] advancedAnalysis[m_]:= Module[{tmp=singleCellsAnalysis[m,2],i,m1,returnvalue}, returnvalue={}; For[i=1, i\[LessEqual] Length[tmp],i++,{ m1= ReplacePart[m,legalvalues[m,tmp[[i,1]],tmp[[i,2]]][[1]], {tmp[[i,1]],tmp[[i,2]]}]; If[((!checkSudokuTwoIdenticalOptions[m1]) || (!checkSudokuZeroOptions[ m1])),{returnvalue= List[List[tmp[[i,1]],tmp[[i,2]], legalvalues[m,tmp[[i,1]],tmp[[i,2]]][[2]]]];Break[]}]; m1= ReplacePart[m,legalvalues[m,tmp[[i,1]],tmp[[i,2]]][[2]], {tmp[[i,1]],tmp[[i,2]]}]; If[((!checkSudokuTwoIdenticalOptions[m1]) || (!checkSudokuZeroOptions[ m1])),{returnvalue= List[List[tmp[[i,1]],tmp[[i,2]], legalvalues[m,tmp[[i,1]],tmp[[i,2]]][[1]]]];Break[];}]; }]; returnvalue ] solveSudokoAdvanced[m_]:= Module[{tmp}, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoAdvanced[ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoAdvanced[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoAdvanced[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoAdvanced[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=advancedAnalysis[m])]>0, Print["Advanced ",tmp]; solveSudokoAdvanced[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], m ] ] ] ] ] ] solveSudokoAdvancedBifurcated[m_]:= Module[{tmp}, If[!MemberQ[Flatten[m],0], Print["Found! "];Print[MatrixForm[m]];m, If[!checkSudokuLegal[m], Print["Shouldn't happen - illegal matrix found!"]; Print[MatrixForm[m]];m, If[!checkSudokuZeroOptions[m], Print["No way to put a number in one of the structures!"]; Print[MatrixForm[m]];m, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=advancedAnalysis[m])]>0, Print["Advanced ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[(tmp=bifurcationAdvanced[m])[[1]]===2, Print["Found! "];Print[MatrixForm[tmp[[2]]]];tmp[[2]], If[Length[(tmp[[2]])]>0, Print["Bifurcation ",tmp]; solveSudokoAdvancedBifurcated[ ReplacePart[m, tmp[[2]][[1,3]],{tmp[[2]][[1,1]], tmp[[2]][[1,2]]}]], m ] ] ] ] ] ] ] ] ] ] ] bifurcationAdvanced[m_]:= Module[{tmp=singleCellsAnalysis[m,2],solution,tmplist}, (* singleCellsAnalysis[m,2] lists the positions of non- empty cells cells with exactly two legal values *) If[Length[tmp]>0, tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["Checking Bifurcation with ",tmplist]; If[MemberQ[ Flatten[(solution= solveSudokoAdvancedBifurcated[ ReplacePart[m, legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]],{tmp[[1,1]], tmp[[1,2]]}]])],0], (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["alternative guess chosen ",tmplist]; List[1,tmplist]), (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess chosen ",tmplist]; List[2,solution]) ],{1,{}} ] ] randompermutation[set_]:= Permutations[set][[Random[Integer,{1,Length[Permutations[set]]}]]] restoptions[set_]:=Complement[{1,2,3,4,5,6,7,8,9},set] Off[General::spell] (* just to stop the warning beeps in defining "createSudoku" *) Off[General::spell1] createSudoku[]:= Module[{line1,line2,line3,line2a,line2b,line2c,line3a,line3b,line3c,i,j=0, okFlag=False,sudoku1,sudoku2}, While[((!okFlag) && ((j++)<100)),{ line1=randompermutation[{1,2,3,4,5,6,7,8,9}]; i=0; line2c={};While[(((i++)<100) && (Length[line2c]<3)),{ line2a=Take[ randompermutation[restoptions[Take[line1,{1,3}]]],{1,3}]; line2b=Take[ randompermutation[ restoptions[Union[Take[line1,{4,6}],line2a]]],{1,3}]; line2c=randompermutation[ restoptions[Union[Take[line1,{7,9}],line2a,line2b]]]; }]; If[i\[GreaterEqual]100,Continue[{}]]; i=0; line3c={};While[(((i++)<100) && (Length[line3c]<3)),{ line3a=randompermutation[ restoptions[Union[Take[line1,{1,3}],line2a]]]; line3b=randompermutation[ restoptions[Union[Take[line1,{4,6}],line2b,line3a]]]; line3c=randompermutation[ restoptions[ Union[Take[line1,{7,9}],line2c,line3a,line3b]]]; }]; If[i\[GreaterEqual]100,Continue[{}]]; sudoku1=Table[Table[0,{r,9},{c,9}] ]; sudoku1=ReplacePart[sudoku1,line1,1]; line2=Join[line2a,line2b,line2c]; line3=Join[line3a,line3b,line3c]; sudoku1=ReplacePart[sudoku1,line2,2]; sudoku1=ReplacePart[sudoku1,line3,3]; sudoku1=Transpose[sudoku1]; line1=Join[Take[sudoku1[[1]],{1,3}], Take[randompermutation[ restoptions[Take[sudoku1[[1]],{1,3}]]],{1,6}]]; i=0; line2c={};While[(((i++)<100) && (Length[line2c]<3)),{ line2a=Take[sudoku1[[2]],{1,3}]; line2b=Take[ randompermutation[ restoptions[Union[Take[line1,{4,6}],line2a]]],{1,3}]; line2c=randompermutation[ restoptions[Union[Take[line1,{7,9}],line2a,line2b]]]; }]; If[i\[GreaterEqual]100,Continue[{}]]; i=0; line3c={};While[(((i++)<100) && (Length[line3c]<3)),{ line3a=Take[sudoku1[[3]],{1,3}]; line3b=randompermutation[ restoptions[Union[Take[line1,{4,6}],line2b,line3a]]]; line3c=randompermutation[ restoptions[ Union[Take[line1,{7,9}],line2c,line3a,line3b]]]; }]; If[i\[GreaterEqual]100,Continue[{}]]; okFlag=True; }] If[j\[GreaterEqual]10,{Return[{}]}]; sudoku1=ReplacePart[sudoku1,line1,1]; line2=Join[line2a,line2b,line2c]; line3=Join[line3a,line3b,line3c]; sudoku1=ReplacePart[sudoku1,line2,2]; sudoku1=ReplacePart[sudoku1,line3,3]; Print["End of phase 1"]; Print[MatrixForm[sudoku1]]; Print["is the matrix legal? - ",checkSudokuLegal[sudoku1]]; okFlag=False;j=0; While[((!okFlag) && ((j++)<10)),{ sudoku2=sudoku1;sudokutmp1243=sudoku2; If[Length[legalvalues[sudoku2,4,4]]\[Equal]0,Continue[], sudoku2=ReplacePart[sudoku2, legalvalues[sudoku2,4, 4][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,4]]}]]],{4,4}]]; If[Length[legalvalues[sudoku2,4,5]]\[Equal]0,Continue[], sudoku2=ReplacePart[sudoku2, legalvalues[sudoku2,4, 5][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,5]]}]]],{4,5}]]; If[Length[legalvalues[sudoku2,4,6]]\[Equal]0,Continue[], sudoku2=ReplacePart[sudoku2, legalvalues[sudoku2,4, 6][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,6]]}]]],{4,6}]]; If[Length[legalvalues[sudoku2,4,7]]\[Equal]0,Continue[], sudoku2=ReplacePart[sudoku2, legalvalues[sudoku2,4, 7][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,7]]}]]],{4,7}]]; If[Length[legalvalues[sudoku2,4,8]]\[Equal]0,Continue[], sudoku2=ReplacePart[sudoku2, legalvalues[sudoku2,4, 8][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,8]]}]]],{4,8}]]; If[Length[legalvalues[sudoku2,4,9]]\[Equal]0,Continue[], sudoku2= ReplacePart[sudoku2, legalvalues[sudoku2,4, 9][[Random[ Integer,{1,Length[legalvalues[sudoku2,4,9]]}]]],{4,9}]]; okFlag=True }]; If[j\[GreaterEqual]10,Return[{}]]; Print["End of phase 2"]; Print[MatrixForm[sudoku2]]; Print["is the matrix legal? - ",checkSudokuLegal[sudoku2]]; sudoku1=solveSudokoBasicBifurcated[sudoku2]; Print["End of phase 3"]; Print[MatrixForm[sudoku1]]; Print["is the matrix legal? - ",checkSudokuLegal[sudoku1]]; sudoku1 ] On[General::spell] On[General::spell1] solveSudokoBasicBifurcatedCheckTwo[m_]:= Module[{tmp}, If[!MemberQ[Flatten[m],0], Print["Found! "];Print[MatrixForm[m]];m, If[!checkSudokuLegal[m], Print["Shouldn't happen - illegal matrix found!"]; Print[MatrixForm[m]];m, If[!checkSudokuZeroOptions[m], Print["No way to put a number in one of the structures!"]; Print[MatrixForm[m]];m, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[(tmp=bifurcationCheckTwo[m])[[1]]===2, Print["Found! "];Print[MatrixForm[tmp[[2]]]];tmp[[2]], If[Length[(tmp[[2]])]>0, Print["Bifurcation ",tmp]; solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m, tmp[[2]][[1,3]],{tmp[[2]][[1,1]], tmp[[2]][[1,2]]}]], m ] ] ] ] ] ] ] ] ] ] bifurcationCheckTwo[m_]:= Module[{tmp=singleCellsAnalysis[m,2],solution,tmplist}, (* singleCellsAnalysis[m,2] lists the positions of non- empty cells cells with exactly two legal values *) If[Length[tmp]>0, tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["Checking Bifurcation with first guess",tmplist]; If[MemberQ[ Flatten[(solution= solveSudokoBasicBifurcatedCheckTwo[ ReplacePart[m, legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]],{tmp[[1,1]], tmp[[1,2]]}]])],0], (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess was mistaken ",tmplist]; tmplist=List[ List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["Checking Bifurcation with second guess",tmplist]; List[1,tmplist]), (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess was correct ",tmplist]; tmplist=List[ List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["Checking Bifurcation with second guess",tmplist]; List[1,tmplist]) ],{1,{}} ] ] solveSudokoAdvancedBifurcatedCheckTwo[m_]:= Module[{tmp}, If[!MemberQ[Flatten[m],0], Print["Found! "];Print[MatrixForm[m]];m, If[!checkSudokuLegal[m], Print["Shouldn't happen - illegal matrix found!"]; Print[MatrixForm[m]];m, If[!checkSudokuZeroOptions[m], Print["No way to put a number in one of the structures!"]; Print[MatrixForm[m]];m, If[Length[(tmp=singleCellsAnalysisCount1[m])]>0, Print["Cells ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleRowsAnalysisCount1[m])]>0, Print["Rows ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleColsAnalysisCount1[m])]>0, Print["Cols ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=singleGridsAnalysisCount1[m])]>0, Print["Grids ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[Length[(tmp=advancedAnalysis[m])]>0, Print["Advanced ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m,tmp[[1,3]],{tmp[[1,1]],tmp[[1,2]]}]], If[(tmp=bifurcationAdvancedCheckTwo[m])[[1]]===2, Print["Found! "];Print[MatrixForm[tmp[[2]]]];tmp[[2]], If[Length[(tmp[[2]])]>0, Print["Bifurcation ",tmp]; solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m, tmp[[2]][[1,3]],{tmp[[2]][[1,1]], tmp[[2]][[1,2]]}]], m ] ] ] ] ] ] ] ] ] ] ] bifurcationAdvancedCheckTwo[m_]:= Module[{tmp=singleCellsAnalysis[m,2],solution,tmplist}, (* singleCellsAnalysis[m,2] lists the positions of non- empty cells cells with exactly two legal values *) If[Length[tmp]>0, tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["Checking Bifurcation with first guess",tmplist]; If[MemberQ[ Flatten[(solution= solveSudokoAdvancedBifurcatedCheckTwo[ ReplacePart[m, legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]],{tmp[[1,1]], tmp[[1,2]]}]])],0], (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess was mistaken ",tmplist]; tmplist=List[ List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["Checking Bifurcation with second guess",tmplist]; List[1,tmplist]), (tmplist= List[List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[1]]]]; Print["first guess was correct ",tmplist]; tmplist=List[ List[tmp[[1,1]],tmp[[1,2]], legalvalues[m,tmp[[1,1]],tmp[[1,2]]][[2]]]]; Print["Checking Bifurcation with second guess",tmplist]; List[1,tmplist]) ],{1,{}} ] ]