36 lines
1.1 KiB
Haskell
36 lines
1.1 KiB
Haskell
module Main (main) where
|
|
import qualified Data.Text.IO as Text
|
|
import Text.Megaparsec (errorBundlePretty, runParser)
|
|
import qualified Data.Text as Text
|
|
import qualified Sudoku
|
|
import qualified Sudoku.Solve as Solve
|
|
import qualified Sudoku.Render as Render
|
|
import Sudoku.State (State)
|
|
import Options.Applicative (subparser, command, info, Parser, progDesc, execParser)
|
|
import System.Exit (exitFailure)
|
|
|
|
data Mode
|
|
= Solve
|
|
| Hints
|
|
|
|
parseMode :: Parser Mode
|
|
parseMode = subparser
|
|
( command "solve" (info (pure Solve) (progDesc "Solve the supplied sudoku entirely."))
|
|
<> command "hints" (info (pure Hints) (progDesc "Show hints for the sudoku."))
|
|
)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
mode <- execParser (info parseMode mempty)
|
|
input <- Text.getContents
|
|
sudoku <- case runParser Sudoku.parse "<stdin>" input of
|
|
Left errorBundle -> (Text.putStr . Text.pack . errorBundlePretty $ errorBundle) >> exitFailure
|
|
Right s -> pure s
|
|
|
|
Text.putStr $ case mode of
|
|
Solve -> Text.pack . show $ Solve.solve State sudoku
|
|
Hints -> let
|
|
hints = Solve.hints State sudoku
|
|
|
|
in Render.hints hints
|
|
|