hasdoku/src/Sudoku.hs

187 lines
5.9 KiB
Haskell
Raw Normal View History

2018-06-17 00:12:23 +05:30
module Main where
2018-06-21 11:32:32 +05:30
import Control.Applicative ((<|>))
import Data.Function ((&))
2018-06-17 16:24:46 +05:30
import qualified Control.Monad
2018-06-17 00:12:23 +05:30
import qualified Data.Char
2018-06-17 16:24:46 +05:30
import qualified Data.Function
2018-06-17 00:12:23 +05:30
import qualified Data.List.Split
import qualified Data.List
import qualified Data.Map.Strict as Map
2018-06-30 12:57:01 +05:30
import qualified Data.Word
import qualified Data.Bits
fixM :: (Eq t, Monad m) => (t -> m t) -> t -> m t
fixM f x = f x >>= \x' -> if x' == x then return x else fixM f x'
2018-06-17 00:12:23 +05:30
2018-06-30 12:57:01 +05:30
setBits :: Data.Word.Word16 -> [Data.Word.Word16] -> Data.Word.Word16
setBits = Data.List.foldl' (Data.Bits..|.)
data Cell = Fixed Data.Word.Word16
| Possible Data.Word.Word16
deriving (Show, Eq)
2018-06-17 00:12:23 +05:30
type Row = [Cell]
type Grid = [Row]
isPossible :: Cell -> Bool
isPossible (Possible _) = True
isPossible _ = False
2018-06-17 00:12:23 +05:30
readGrid :: String -> Maybe Grid
2018-06-17 11:54:22 +05:30
readGrid s
| length s == 81 = traverse (traverse readCell) . Data.List.Split.chunksOf 9 $ s
2018-06-17 11:54:22 +05:30
| otherwise = Nothing
2018-06-17 00:12:23 +05:30
where
2018-06-30 12:57:01 +05:30
allBitsSet = 1022
readCell '.' = Just $ Possible allBitsSet
2018-06-17 00:12:23 +05:30
readCell c
2018-06-30 12:57:01 +05:30
| Data.Char.isDigit c && c > '0' = Just . Fixed . Data.Bits.bit . Data.Char.digitToInt $ c
2018-06-17 00:12:23 +05:30
| otherwise = Nothing
showGrid :: Grid -> String
showGrid = unlines . map (unwords . map showCell)
where
2018-06-30 12:57:01 +05:30
showCell (Fixed x) = show . Data.Bits.countTrailingZeros $ x
showCell _ = "."
2018-06-17 00:12:23 +05:30
showGridWithPossibilities :: Grid -> String
showGridWithPossibilities = unlines . map (unwords . map showCell)
where
2018-06-30 12:57:01 +05:30
showCell (Fixed x) = (show . Data.Bits.countTrailingZeros $ x) ++ " "
2018-06-17 00:12:23 +05:30
showCell (Possible xs) =
2018-06-30 12:57:01 +05:30
"[" ++ map (\i -> if Data.Bits.testBit xs i then Data.Char.intToDigit i else ' ') [1..9] ++ "]"
2018-06-17 00:12:23 +05:30
2018-06-30 12:57:01 +05:30
exclusivePossibilities :: [Cell] -> [Data.Word.Word16]
exclusivePossibilities row =
row
& zip [1..9]
& filter (isPossible . snd)
& Data.List.foldl'
(\acc ~(i, Possible xs) ->
2018-06-30 12:57:01 +05:30
Data.List.foldl'
(\acc' n -> if Data.Bits.testBit xs n then Map.insertWith prepend n [i] acc' else acc')
acc
[1..9])
Map.empty
& Map.filter ((< 4) . length)
& Map.foldlWithKey'(\acc x is -> Map.insertWith prepend is [x] acc) Map.empty
& Map.filterWithKey (\is xs -> length is == length xs)
& Map.elems
2018-06-30 12:57:01 +05:30
& map (Data.List.foldl' Data.Bits.setBit Data.Bits.zeroBits)
where
prepend ~[y] ys = y:ys
2018-06-30 12:57:01 +05:30
makeCell :: Data.Word.Word16 -> Maybe Cell
makeCell ys
| ys == Data.Bits.zeroBits = Nothing
| Data.Bits.popCount ys == 1 = Just $ Fixed ys
| otherwise = Just $ Possible ys
pruneCellsByFixed :: [Cell] -> Maybe [Cell]
pruneCellsByFixed cells = traverse pruneCell cells
2018-06-17 16:24:46 +05:30
where
2018-06-30 12:57:01 +05:30
fixeds = setBits Data.Bits.zeroBits [x | Fixed x <- cells]
2018-06-30 12:57:01 +05:30
pruneCell (Possible xs) = makeCell (xs Data.Bits..&. Data.Bits.complement fixeds)
pruneCell x = Just x
pruneCellsByExclusives :: [Cell] -> Maybe [Cell]
pruneCellsByExclusives cells = case exclusives of
[] -> Just cells
_ -> traverse pruneCell cells
where
exclusives = exclusivePossibilities cells
2018-06-30 12:57:01 +05:30
allExclusives = setBits Data.Bits.zeroBits exclusives
pruneCell cell@(Fixed _) = Just cell
pruneCell cell@(Possible xs)
| intersection `elem` exclusives = makeCell intersection
| otherwise = Just cell
where
2018-06-30 12:57:01 +05:30
intersection = xs Data.Bits..&. allExclusives
pruneCells :: [Cell] -> Maybe [Cell]
pruneCells cells = fixM pruneCellsByFixed cells >>= fixM pruneCellsByExclusives
2018-06-17 16:24:46 +05:30
subGridsToRows :: Grid -> Grid
subGridsToRows =
concatMap (\rows -> let [r1, r2, r3] = map (Data.List.Split.chunksOf 3) rows
2018-06-17 16:24:46 +05:30
in zipWith3 (\a b c -> a ++ b ++ c) r1 r2 r3)
. Data.List.Split.chunksOf 3
pruneGrid' :: Grid -> Maybe Grid
2018-06-17 16:24:46 +05:30
pruneGrid' grid =
traverse pruneCells grid
>>= fmap Data.List.transpose . traverse pruneCells . Data.List.transpose
>>= fmap subGridsToRows . traverse pruneCells . subGridsToRows
2018-06-17 16:24:46 +05:30
pruneGrid :: Grid -> Maybe Grid
pruneGrid = fixM pruneGrid'
2018-06-17 16:24:46 +05:30
2018-06-26 11:48:14 +05:30
isGridFilled :: Grid -> Bool
isGridFilled grid = null [ () | Possible _ <- concat grid ]
2018-06-21 11:32:32 +05:30
2018-06-26 11:48:14 +05:30
isGridInvalid :: Grid -> Bool
isGridInvalid grid =
2018-06-17 16:24:46 +05:30
any isInvalidRow grid
|| any isInvalidRow (Data.List.transpose grid)
|| any isInvalidRow (subGridsToRows grid)
2018-06-17 16:24:46 +05:30
where
2018-06-21 11:32:32 +05:30
isInvalidRow row =
2018-06-30 12:57:01 +05:30
let fixeds = [x | Fixed x <- row]
emptyPossibles = [() | Possible x <- row, x == Data.Bits.zeroBits]
2018-06-21 11:32:32 +05:30
in hasDups fixeds || not (null emptyPossibles)
2018-06-17 16:24:46 +05:30
2018-06-21 11:32:32 +05:30
hasDups l = hasDups' l []
hasDups' [] _ = False
hasDups' (y:ys) xs
| y `elem` xs = True
| otherwise = hasDups' ys (y:xs)
2018-06-17 16:24:46 +05:30
2018-06-26 11:48:14 +05:30
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)
2018-06-21 11:32:32 +05:30
where
2018-06-30 12:57:01 +05:30
possibilityCount (Possible xs) = Data.Bits.popCount xs
2018-06-26 11:48:14 +05:30
possibilityCount (Fixed _) = 1
2018-06-21 11:32:32 +05:30
2018-06-30 12:57:01 +05:30
fixCell ~(i, Possible xs) =
let x = Data.Bits.countTrailingZeros xs
in case makeCell (Data.Bits.clearBit xs x) of
Nothing -> error "Impossible case"
Just cell -> (i, Fixed (Data.Bits.bit x), cell)
2018-06-21 11:32:32 +05:30
2018-06-26 11:48:14 +05:30
replace2D :: Int -> a -> [[a]] -> [[a]]
2018-06-21 11:32:32 +05:30
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..]]
2018-06-17 00:12:23 +05:30
2018-06-26 11:48:14 +05:30
solve :: Grid -> Maybe Grid
solve grid = pruneGrid grid >>= solve'
where
2018-06-27 15:27:34 +05:30
solve' g
| isGridInvalid g = Nothing
| isGridFilled g = Just g
| otherwise =
let (grid1, grid2) = nextGrids g
2018-06-26 11:48:14 +05:30
in solve grid1 <|> solve grid2
main :: IO ()
2018-06-17 16:24:46 +05:30
main = do
2018-06-26 11:48:14 +05:30
inputs <- lines <$> getContents
Control.Monad.forM_ inputs $ \input ->
case readGrid input of
Nothing -> putStrLn "Invalid input"
2018-06-21 11:32:32 +05:30
Just grid -> case solve grid of
2018-06-26 11:48:14 +05:30
Nothing -> putStrLn "No solution found"
2018-06-17 16:24:46 +05:30
Just grid' -> putStrLn $ showGrid grid'