-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttempt1.hs
More file actions
91 lines (76 loc) · 2.63 KB
/
Copy pathAttempt1.hs
File metadata and controls
91 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Attempt1
( solution,
)
where
import Control.Applicative
import Control.Monad
import Control.Monad.Logic
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (isNothing)
import Problem
data Attempt = HasQueen | Eliminated
deriving (Show, Eq)
type Partial = Map (Row, Column) Attempt
insertIfAbsent :: (Ord k) => k -> a -> Map k a -> Map k a
insertIfAbsent = Map.insertWith (\_ x -> x)
placeQueen :: Problem -> (Row, Column) -> Partial -> Partial
placeQueen problem (x, y) partial =
elimCorners (x, y) problem
. elimColumn y problem
. elimRow x problem
. elimColor color problem
$ newBoard
where
newBoard = Map.insert (x, y) HasQueen partial
color = problem ! (x, y)
elimCorners :: (Row, Column) -> Problem -> Partial -> Partial
elimCorners (x, y) problem = foldr (.) id elimFns
where
n = size problem
elimFns =
[ insertIfAbsent (x', y') Eliminated
| x' <- [x - 1, x + 1],
x' >= 0,
x' < n,
y' <- [y - 1, y + 1],
y' >= 0,
y' < n
]
elimColumn :: Column -> Problem -> Partial -> Partial
elimColumn y problem partial = foldr elimRow' partial [0 .. size problem - 1]
where
elimRow' x' = insertIfAbsent (x', y) Eliminated
elimRow :: Row -> Problem -> Partial -> Partial
elimRow x problem partial = foldr elimColumn' partial [0 .. size problem - 1]
where
elimColumn' y' = insertIfAbsent (x, y') Eliminated
elimColor :: Color -> Problem -> Partial -> Partial
elimColor color problem = foldr (.) id elimFns
where
n = size problem
elimFns = [insertIfAbsent (x, y) Eliminated | x <- [0 .. n - 1], y <- [0 .. n - 1], problem ! (x, y) == color]
choose :: (MonadLogic m, Foldable t) => t a -> m a
choose = foldr ((<|>) . pure) empty
candidate :: (MonadLogic m) => Problem -> Partial -> m (Row, Column)
candidate problem partial = do
let n = size problem
x <- choose [0 .. n - 1]
y <- choose [0 .. n - 1]
-- only pick unmarked cells
guard (isNothing (Map.lookup (x, y) partial))
pure (x, y)
-- do an action n times
repeatM :: (Monad m) => Int -> (a -> m a) -> a -> m a
repeatM n f = foldr (>=>) pure (replicate n f)
extend :: (MonadLogic m) => Problem -> Partial -> m Partial
extend problem partial = do
pos <- candidate problem partial
-- place a queen on the candidate cell
pure $ placeQueen problem pos partial
queenView :: Partial -> [(Row, Column)]
queenView partial = [(x, y) | ((x, y), status) <- Map.toList partial, status == HasQueen]
solution :: (MonadLogic m) => Problem -> m [(Row, Column)]
solution problem = do
endState <- repeatM (size problem) (extend problem) Map.empty
pure $ queenView endState