Created
October 11, 2015 03:30
-
-
Save UnluckyNinja/56ac9bfc1376320bbd88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def i = System.currentTimeMillis() | |
def GRIDS = [1,2,3,4,5,6,7,8,9].permutations().asImmutable() | |
println(System.currentTimeMillis() - i) | |
println GRIDS.size() | |
def result = 0G | |
def rules = [[1,0,1, | |
0,1,0, | |
1,0,1], | |
[1,0,0, | |
0,1,0, | |
0,0,1], | |
[0,0,1, | |
0,1,0, | |
1,0,0], | |
[0,0,1, | |
0,1,0, | |
1,0,0], | |
[1,0,0, | |
0,1,0, | |
0,0,1]] | |
/*grids.filter1().findAll().each{ | |
filter2().findAll().each{ | |
} | |
}*/ | |
def ruleFilter = { rule, candidates -> | |
candidates.findAll{ | |
[rule, it].transpose().every{ | |
it[0] == 0 ?: it[1] != 1 | |
} | |
} | |
}.memoize() | |
def GRID_CANDIDATES = rules.collect{ rule -> | |
ruleFilter(rule, GRIDS) | |
}.asImmutable() | |
/** | |
* sudokuRule is a map. this clousre filters all candidates that satisfy sudoku rule | |
* H: left & right | |
* V: up & down | |
**/ | |
def sudokuFilter = { Map sudokuRule, candidates-> | |
def resultCandidates = candidates | |
if(sudokuRule.H){ | |
resultCandidates = resultCandidates.findAll{ candidate -> | |
sudokuRule.H.every{ grid -> | |
[grid.collate(3), candidate.collate(3)].transpose().every{ | |
it[0].disjoint it[1] | |
} | |
} | |
} | |
} | |
if(sudokuRule.V){ | |
resultCandidates = resultCandidates.findAll{ candidate -> | |
sudokuRule.V.every{ grid -> | |
[grid.collate(3).transpose(), candidate.collate(3).transpose()].transpose().every{ | |
it[0].disjoint it[1] | |
} | |
} | |
} | |
} | |
resultCandidates | |
}.memoize() | |
def generateSudokuRule = { filledGrids, int candidateGrid -> | |
switch(candidateGrid){ | |
case 1: | |
return [H:[] as Set, V:[] as Set] | |
case 2: | |
return [H:[] as Set, V:[] as Set] | |
case 3: | |
return [H:[filledGrids[1]] as Set, V:[] as Set] | |
case 4: | |
return [H:[] as Set, V:[filledGrids[1]] as Set] | |
case 5: | |
return [H:[filledGrids[3]] as Set, V:[filledGrids[2]] as Set] | |
case 6: | |
return [H:filledGrids[1,2] as Set, V:[filledGrids[0]] as Set] | |
case 7: | |
return [H:[filledGrids[0]] as Set, V:filledGrids[1,3] as Set] | |
case 8: | |
return [H:filledGrids[0,6] as Set, V:[filledGrids[2]] as Set] | |
case 9: | |
return [H:filledGrids[3,4] as Set, V:filledGrids[0,5] as Set] | |
default: | |
throw new IllegalArgumentException('Rulegen Range 1..9') | |
} | |
} | |
/******************* | |
* 步骤:从中间开始,先过滤掉不能放1的,每次过滤以之前为基准 | |
* [2, 6, 3, | |
* 7, 1, 8, | |
* 4, 9, 5 ] | |
* | |
* [1, 2, 3, | |
* 4, 5, 6, | |
* 7, 8, 9 ] | |
* | |
* | |
*******************/ | |
def Filled_Grids = [] | |
def GridIndex = 1 | |
GRID_CANDIDATES[0].findAll{ grid1 -> | |
grid1[1].is(1) | |
}.each{ grid1 -> | |
Filled_Grids.push grid1 | |
GridIndex++ | |
GRID_CANDIDATES[1].each{ grid2 -> | |
Filled_Grids.push grid2 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRID_CANDIDATES[2]).each{ grid3 -> | |
Filled_Grids.push grid3 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRID_CANDIDATES[3]).each{ grid4 -> | |
Filled_Grids.push grid4 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRID_CANDIDATES[4]).each{ grid5 -> | |
Filled_Grids.push grid5 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRIDS).each{ grid6 -> | |
Filled_Grids.push grid6 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRIDS).each{ grid7 -> | |
Filled_Grids.push grid7 | |
GridIndex++ | |
sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRIDS).each{ grid8 -> | |
result += sudokuFilter(generateSudokuRule(Filled_Grids, GridIndex), GRIDS).size() | |
print "$result \r" | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} | |
GridIndex-- | |
Filled_Grids.pop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment