forked from mattiegraf/312-Kingdom-of-Zed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koz_4x4.hs
81 lines (59 loc) · 2.65 KB
/
koz_4x4.hs
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
import Data.List
-- assume a solution exists
zed conds@(top,_,_,_) =
let n = (length top)
in head (filter (isBoardValid conds) (genBoards n n []))
isBoardValid (top,right,bottom,left) board = topIsValid && rightIsValid && bottomIsValid && leftIsValid
where
ns = [0..(length top)-1]
topIsValid = (and [dirValid (top!!n) (getCol n board) | n <- ns])
rightIsValid = (and [dirValid (right!!n) (reverse (board!!n)) | n <- ns])
bottomIsValid = (and [dirValid ((reverse bottom)!!n) (reverse (getCol n board)) | n <- ns])
leftIsValid = (and [dirValid ((reverse left)!!n) (board!!n) | n <- ns])
dirValid cond [x,y]
| x == y = False
| cond > 2 = False
| cond == 1 = x > y
| otherwise = x < y
dirValid cond lst@(x:y:xs)
| x == y = False
| cond == 1 = (x == length lst)
| x < y = dirValid (cond-1) (y:xs)
| x > y = dirValid cond (x:xs)
genBoards n 0 acc = acc
genBoards n c [] = genBoards n (c-1) (genChildBoards n [])
genBoards n c acc =
foldr (++) [] (map (genChildBoards n) (genBoards n (c-1) acc))
genChildBoards n board =
[x:board | x <- permutations [1..n], not (x `elem` board)]
-- genBoards = [[r1, r2, r3,r4] | r1 <- n,
-- r2 <- (delete r1 n),
-- r3 <- (delete r1 (delete r2 n)),
-- r4 <- (delete r1 (delete r2 (delete r3 n))),
-- (r1!!0) /= (r2!!0),
-- (r1!!0) /= (r3!!0),
-- (r1!!0) /= (r4!!0),
-- (r2!!0) /= (r3!!0),
-- (r2!!0) /= (r4!!0),
-- (r3!!0) /= (r4!!0),
-- (r1!!1) /= (r2!!1),
-- (r1!!1) /= (r3!!1),
-- (r1!!1) /= (r4!!1),
-- (r2!!1) /= (r3!!1),
-- (r2!!1) /= (r4!!1),
-- (r3!!1) /= (r4!!1),
-- (r1!!2) /= (r2!!2),
-- (r1!!2) /= (r3!!2),
-- (r1!!2) /= (r4!!2),
-- (r2!!2) /= (r3!!2),
-- (r2!!2) /= (r4!!2),
-- (r3!!2) /= (r4!!2)
-- ]
-- where n = permutations [1..4]
-- !!! add in type declaration?
--assume valid board, valid n
--end case
getCol n [] = []
--non empty board
getCol n board =
((head(board))!!n):(getCol n (tail(board)))