Created
January 22, 2019 17:40
-
-
Save abhin4v/bfd3902acdaddd84357f41529c121350 to your computer and use it in GitHub Desktop.
This file contains 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
nextGrids :: Grid -> (Grid, Grid) | |
nextGrids grid = | |
let (i, first@(Fixed _), rest) = | |
fixCell | |
. Data.List.minimumBy (compare `Data.Function.on` (possibilityCount . snd)) | |
. filter (isPossible . snd) | |
. zip [0..] | |
. concat | |
$ grid | |
in (replace2D i first grid, replace2D i rest grid) | |
where | |
isPossible (Possible _) = True | |
isPossible _ = False | |
possibilityCount (Possible xs) = length xs | |
possibilityCount (Fixed _) = 1 | |
fixCell (i, Possible [x, y]) = (i, Fixed x, Fixed y) | |
fixCell (i, Possible (x:xs)) = (i, Fixed x, Possible xs) | |
fixCell _ = error "Impossible case" | |
replace2D :: Int -> a -> [[a]] -> [[a]] | |
replace2D i v = | |
let (x, y) = (i `quot` 9, i `mod` 9) in replace x (replace y (const v)) | |
replace p f xs = [if i == p then f x else x | (x, i) <- zip xs [0..]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment