From b2a0dd88172760f91a73f3d0d0dc93b548053f46 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Sun, 17 Jun 2018 00:12:23 +0530 Subject: [PATCH] Adds types, and read-show functions --- app/Main.hs | 6 ------ package.yaml | 12 ++++-------- src/Lib.hs | 6 ------ src/Sudoku.hs | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 20 deletions(-) delete mode 100644 app/Main.hs delete mode 100644 src/Lib.hs create mode 100644 src/Sudoku.hs diff --git a/app/Main.hs b/app/Main.hs deleted file mode 100644 index de1c1ab..0000000 --- a/app/Main.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Main where - -import Lib - -main :: IO () -main = someFunc diff --git a/package.yaml b/package.yaml index 244603f..852595f 100644 --- a/package.yaml +++ b/package.yaml @@ -14,23 +14,19 @@ description: Please see the README on GitHub at = 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: [] diff --git a/src/Lib.hs b/src/Lib.hs deleted file mode 100644 index d36ff27..0000000 --- a/src/Lib.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Lib - ( someFunc - ) where - -someFunc :: IO () -someFunc = putStrLn "someFunc" diff --git a/src/Sudoku.hs b/src/Sudoku.hs new file mode 100644 index 0000000..70c91f4 --- /dev/null +++ b/src/Sudoku.hs @@ -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 \ No newline at end of file