Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
65
Task/Nonogram-solver/Mathematica/nonogram-solver.math
Normal file
65
Task/Nonogram-solver/Mathematica/nonogram-solver.math
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
ClearAll[VisualizeGrid, Possibilities, TryRow, TryColumn]
|
||||
VisualizeGrid[candgrid_List] := StringRiffle[StringJoin/@Replace[candgrid,{{0}->" ",{1}->"#",{0,1}|{1,0}->"."},{2}],"\n"]
|
||||
Possibilities[clues_List, len_Integer] := Module[{spaces, numclue, spacecands, cands},
|
||||
numclue = Length[clues];
|
||||
spaces = len - Total[clues];
|
||||
spacecands = IntegerPartitions[spaces, {numclue - 1}];
|
||||
spacecands = DeleteDuplicates[Catenate[Permutations /@ spacecands]];
|
||||
cands = Catenate[Riffle[ConstantArray[1, #] & /@ clues, ConstantArray[0, #] & /@ #]] & /@ spacecands;
|
||||
spacecands = IntegerPartitions[spaces, {numclue}];
|
||||
spacecands = DeleteDuplicates[Catenate[Permutations /@ spacecands]];
|
||||
cands = Join[cands, Catenate[Riffle[ConstantArray[1, #] & /@ clues, ConstantArray[0, #] & /@ #]] & /@ spacecands];
|
||||
cands = Join[cands, Catenate[Riffle[ConstantArray[0, #] & /@ #, ConstantArray[1, #] & /@ clues]] & /@ spacecands];
|
||||
|
||||
spacecands = IntegerPartitions[spaces, {numclue + 1}];
|
||||
spacecands = DeleteDuplicates[Catenate[Permutations /@ spacecands]];
|
||||
cands = Join[cands, Catenate[Riffle[ConstantArray[0, #] & /@ #, ConstantArray[1, #] & /@ clues]] & /@ spacecands];
|
||||
|
||||
cands
|
||||
]
|
||||
TryRow[candgrid_List, i_Integer, hclues_List] := Module[{row, clue, len, poss, newgrid},
|
||||
row = candgrid[[i]];
|
||||
clue = hclues[[i]];
|
||||
len = Length[row];
|
||||
poss = Possibilities[clue, len];
|
||||
poss //= Select[MatchQ[Alternatives @@@ row]];
|
||||
poss //= Transpose;
|
||||
poss //= Map[Union];
|
||||
newgrid = candgrid;
|
||||
newgrid[[i]] = poss;
|
||||
newgrid
|
||||
]
|
||||
TryColumn[candgrid_List, i_Integer, hclues_List] := Transpose[TryRow[Transpose[candgrid], i, hclues]]
|
||||
puzzles = "C BA CB BB F AE F A B
|
||||
AB CA AE GA E C D C
|
||||
|
||||
F CAC ACAC CN AAA AABB EBB EAA ECCC HCCC
|
||||
D D AE CD AE A DA BBB CC AAB BAA AAB DA AAB AAA BAB AAA CD BBA DA
|
||||
|
||||
CA BDA ACC BD CCAC CBBAC BBBBB BAABAA ABAD AABB BBH BBBD ABBAAA CCEA AACAAB BCACC ACBH DCH ADBE ADBB DBE ECE DAA DB CC
|
||||
BC CAC CBAB BDD CDBDE BEBDF ADCDFA DCCFB DBCFC ABDBA BBF AAF BADB DBF AAAAD BDG CEF CBDB BBB FC
|
||||
|
||||
E BCB BEA BH BEK AABAF ABAC BAA BFB OD JH BADCF Q Q R AN AAN EI H G
|
||||
E CB BAB AAA AAA AC BB ACC ACCA AGB AIA AJ AJ ACE AH BAF CAG DAG FAH FJ GJ ADK ABK BL CM";
|
||||
puzzles = StringSplit[puzzles, "\n\n"];
|
||||
puzzles = StringSplit[#, "\n"] & /@ puzzles;
|
||||
puzzles = Map[StringSplit[#, " "] &, puzzles, {2}];
|
||||
puzzles = Map[Characters, puzzles, {3}];
|
||||
puzzles = puzzles /. Thread[CharacterRange["A", "Z"] -> (ToString /@ Range[26])];
|
||||
puzzles = Map[ToExpression, puzzles, {4}];
|
||||
|
||||
Do[
|
||||
hclues = puzzles[[n, 1]];
|
||||
vclues = puzzles[[n, 2]];
|
||||
{hsize, vsize} = {vclues // Length, hclues // Length};
|
||||
cand = ConstantArray[{0, 1}, {vsize, hsize}];
|
||||
oldcand = {};
|
||||
While[oldcand =!= cand,
|
||||
oldcand = cand;
|
||||
Do[cand = TryRow[cand, i, hclues], {i, Length[hclues]}];
|
||||
Do[cand = TryColumn[cand, i, vclues], {i, Length[vclues]}];
|
||||
];
|
||||
Print@VisualizeGrid[cand]
|
||||
,
|
||||
{n, 4}
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue