-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriver.blunt
67 lines (51 loc) · 2.3 KB
/
river.blunt
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
data Person = Father | Boy | Mother | Girl | Police | Prisoner
deriving (Eq, Show, Ord)
data Side = West | East deriving (Eq, Show)
data Game = G Side [Person] [Person] deriving (Eq, Show)
initialGame = G West (sort [Father, Boy, Boy, Mother, Girl, Girl, Police, Prisoner]) []
isGameEnded (G East [] _) = True
isGameEnded _ = False
candidatePeople (G West w _) = w
candidatePeople (G East _ e) = e
canUseBoat Father = True
canUseBoat Mother = True
canUseBoat Police = True
canUseBoat _ = False
move (G West w e) m = G East (w \\ m) (mergeSorted e m)
move (G East w e) m = G West (mergeSorted w m) (e \\ m)
a ==> b = if a then b else True
canShareBank ps =
let onBank p = elem p ps
in let isAlone p = [p] == ps
in let fatherOk = onBank Father ==> (onBank Mother || not (onBank Girl))
in let motherOk = onBank Mother ==> (onBank Father || not (onBank Boy))
in let prisonerOk = onBank Prisoner ==> (onBank Police || isAlone Prisoner)
in fatherOk && motherOk && prisonerOk
validState (G _ w e) = canShareBank w && canShareBank e
isValidMove g m = any canUseBoat m && validState (move g m)
pairs xs = concatMap (\p -> case p of
(y:ys) -> map (\z -> (y,z)) ys
_ -> []
)
(tails xs)
validMoves g =
let people = candidatePeople g
in let singleMoves = map singleton people
in let pairMoves = map (\(x,y) -> [x,y]) (pairs people)
in filter (isValidMove g) (singleMoves ++ pairMoves)
concatMapMaybe f [] = Nothing
concatMapMaybe f (x:xs) = case f x of
Just y -> Just y
Nothing -> concatMapMaybe f xs
solve () =
let rec solve' vs ts s = if isGameEnded s
then Just (reverse ts)
else concatMapMaybe (\t ->
let s' = move s t
in if elem s' vs
then Nothing
else solve' (s':vs) (t:ts) s') (validMoves s)
in fromJust $ solve' [initialGame] [] initialGame
river () =
let printRow x = print x; print "\n"
in foreach printRow (solve ())