Skip to content

Instantly share code, notes, and snippets.

@presci
Created July 21, 2023 22:33
Show Gist options
  • Save presci/8e25a6dae4ee63d792768e0c002ff0d2 to your computer and use it in GitHub Desktop.
Save presci/8e25a6dae4ee63d792768e0c002ff0d2 to your computer and use it in GitHub Desktop.
import SudUtils
import qualified Data.Vector as V
import Data.Maybe
import Data.List (sort, group)
-- / end initialization code
solve :: Board -> Board
solve vec = V.imap (\i rw -> update i rw) vec
where
update :: Int -> V.Vector Cell -> V.Vector Cell
update rw vec1 = V.imap (\i val -> updateCell rw i val ) vec1
updateCell :: Int -> Int -> Cell -> Cell
updateCell _ _ a@(Cell _) = a
updateCell rw col (Cells x) = case filter (`notElem` (getskiplist rw col vec) ) x of
[x] -> Cell x
a -> Cells a
solve2::Board -> Board
solve2 board = helper01 $ findNextEmptyCell
where
helper01 :: Maybe (Int, Int) -> Board -> Board
helper01 Nothing board = board
helper01 (Just a) board = board
test :: V.Vector (V.Vector Cell)
test = conv2 . V.fromList $ map V.fromList [[0,9,6,4,0,0,2,0,3],
[4,0,0,3,0,0,9,0,0],
[2,0,0,0,9,0,0,7,1],
[9,2,0,0,0,3,0,0,0],
[0,0,5,0,0,0,1,0,0],
[0,0,0,5,0,0,0,3,4],
[5,3,0,0,6,0,0,0,8],
[0,0,2,0,0,8,0,0,6],
[8,0,1,0,0,4,3,9,0]]
test01:: V.Vector (V.Vector Cell)
test01 = conv2 . V.fromList $ map V.fromList [[0,0,0,0,0,7,0,9,4],
[6,9,5,0,8,0,0,0,0],
[4,0,3,0,0,9,0,6,0],
[0,5,0,4,0,0,0,0,6],
[0,0,0,7,6,3,0,0,0],
[2,0,0,0,0,1,0,8,0],
[0,3,0,9,0,0,7,0,8],
[0,0,0,0,1,0,3,2,9],
[9,1,0,3,0,0,0,0,0]]
test02 :: V.Vector (V.Vector Cell)
test02 = conv2 . V.fromList $ map V.fromList [[0,0,0,0,0,6,0,0,8],
[0,0,0,7,8,0,1,0,0],
[0,0,3,0,0,0,0,0,5],
[0,0,1,4,0,0,6,9,2],
[0,0,0,6,0,5,0,0,0],
[6,9,4,0,0,2,7,0,0],
[4,0,0,0,0,0,3,0,0],
[0,0,2,0,6,4,0,0,0],
[3,0,0,9,0,0,0,0,0]]
main::IO()
main = do
let a = solve $ test02
print a
print $ findNextEmptyCell a
{-
[[Cell 8,Cell 2,Cell 1,Cell 6,Cell 3,Cell 7,Cell 5,Cell 9,Cell 4],
[Cell 6,Cell 9,Cell 5,Cell 1,Cell 8,Cell 4,Cell 2,Cell 3,Cell 7],
[Cell 4,Cell 7,Cell 3,Cell 2,Cell 5,Cell 9,Cell 8,Cell 6,Cell 1],
[Cell 3,Cell 5,Cell 9,Cell 4,Cell 2,Cell 8,Cell 1,Cell 7,Cell 6],
[Cell 1,Cell 8,Cell 4,Cell 7,Cell 6,Cell 3,Cell 9,Cell 5,Cell 2],
[Cell 2,Cell 6,Cell 7,Cell 5,Cell 9,Cell 1,Cell 4,Cell 8,Cell 3],
[Cell 5,Cell 3,Cell 2,Cell 9,Cell 4,Cell 6,Cell 7,Cell 1,Cell 8],
[Cell 7,Cell 4,Cell 6,Cell 8,Cell 1,Cell 5,Cell 3,Cell 2,Cell 9],
[Cell 9,Cell 1,Cell 8,Cell 3,Cell 7,Cell 2,Cell 6,Cell 4,Cell 5]]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment