Adds types, and read-show functions

custom-accumulator
Abhinav Sarkar 2018-06-17 00:12:23 +05:30
parent 7fe01ba070
commit b2a0dd8817
4 changed files with 43 additions and 20 deletions

View File

@ -1,6 +0,0 @@
module Main where
import Lib
main :: IO ()
main = someFunc

View File

@ -14,23 +14,19 @@ description: Please see the README on GitHub at <https://github.com/abhi
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
executables:
hasdoku:
main: Main.hs
source-dirs: app
main: Sudoku.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- hasdoku
- split
tests:
hasdoku-test:
main: Spec.hs
source-dirs: test
dependencies:
- hasdoku
dependencies: []

View File

@ -1,6 +0,0 @@
module Lib
( someFunc
) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"

39
src/Sudoku.hs Normal file
View File

@ -0,0 +1,39 @@
module Main where
import qualified Data.Char
import qualified Data.List.Split
import qualified Data.List
data Cell = Fixed Int | Possible [Int] deriving (Show, Eq)
type Row = [Cell]
type Grid = [Row]
possibleVals :: Cell -> [Int]
possibleVals (Fixed x) = [x]
possibleVals (Possible xs) = xs
readGrid :: String -> Maybe Grid
readGrid s = if length s /= 81
then Nothing
else sequence . map (sequence . map readCell) . Data.List.Split.chunksOf 9 $ s
where
readCell '.' = Just $ Possible [1..9]
readCell c
| Data.Char.isDigit c && c > '0' = Just . Fixed . Data.Char.digitToInt $ c
| otherwise = Nothing
showGrid :: Grid -> String
showGrid = unlines . map (unwords . map showCell)
where
showCell (Fixed x) = show x
showCell _ = "."
showGridWithPossibilities :: Grid -> String
showGridWithPossibilities = unlines . map (unwords . map showCell)
where
showCell (Fixed x) = show x ++ " "
showCell (Possible xs) =
(Data.List.foldl' (\acc x -> acc ++ if x `elem` xs then show x else " ") "[" [1..9]) ++ "]"
main = undefined