Solve a Hidato puzzle: Difference between revisions
→{{header|Haskell}}: Made an edit to avoid a Wiki formatting problem (+ hindent)
m (added more related tasks.) |
(→{{header|Haskell}}: Made an edit to avoid a Wiki formatting problem (+ hindent)) |
||
Line 1,355:
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE TupleSections #-}
{-# LANGUAGE Rank2Types #-}
import qualified Data.IntMap as I
import Data.IntMap (IntMap)
Line 1,364:
import Data.Time.Clock
data BoardProblem = Board
{ cells :: IntMap (IntMap Int)
, endVal :: Int▼
, givens :: [Int]
} deriving (Show, Eq)
tupIns x y v m = I.insert x (I.insert y v (I.findWithDefault I.empty x m)) m
tupLookup x y m = I.lookup x m >>= I.lookup y
makeBoard =
(\x ->
x
. zipWith (\y w -> map (y,) $ words w) [0..]▼
}) .
Board (tupIns x y (read v) (cells bd)) ▼
foldl' --'
(if read v > endVal bd then read v else endVal bd)▼
f
(if v=="1" then (x,y) else onePos bd)▼
(Board I.empty
where
f bd (x, (y, v)) =
if v == "."
then bd
else Board
then read v
(if v == "1"
then (x, y)
(read v : givens bd)
hidato brd = listToMaybe $ h 2 (cells brd) (onePos brd) (givens brd)
where
h nval pmap (x,y) gs | nval == endVal brd = [pmap]▼
h nval pmap (x, y) gs
else h (nval+1) pmap (fst $ head nvalAdj) (tail gs)▼
if
then []
| otherwise = hEmptyAdj▼
| not $ null nvalAdj = h (nval + 1) pmap (fst $
lkdUp = map (\(x,y) -> ((x,y),tupLookup x y pmap)) around▼
where
nvalAdj = filter ((==Just nval) . snd) lkdUp▼
around =
, (x + 1, y - 1)
, (x - 1, y)
, (x + 1, y)
, (x - 1, y + 1)
, (x, y + 1)
, (x + 1, y + 1)
]
hEmptyAdj =
concatMap
(\((nx, ny), _) -> h (nval + 1) (tupIns nx ny nval pmap) (nx, ny) gs) $
filter ((== Just 0) . snd) lkdUp
printCellMap cellmap = putStrLn $ concat strings
where
minPos = xyBy I.findMin minimum
xyBy :: (forall a.
xyBy a b = (fst (a
map
, x<-[fst minPos..fst maxPos]]▼
f
f (x,y) = let z = if x == fst maxPos then "\n" else " " in▼
[
| y <- [snd minPos .. snd maxPos]
Just n -> (if n<10 then ' ':show n else show n) ++ z▼
f (x, y) =
let z =
then "\n"
else " "
in case tupLookup x y cellmap of
Nothing -> " " ++ z
Just n ->
(if n < 10
else show n) ++
z
main = do
sample =
[
,
, " 0 0
,
,
,
,
, ". . . . . . 5 0"
]</lang>
{{Out}}
<pre> 0 33 35 0 0
0 0 24 22 0
|