Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import qualified Data.List as List
  2.  
  3. type Cell = Char
  4. type Table = [[Cell]]
  5. type ClueLine = [Int]
  6. type Clues = ([ClueLine], [ClueLine])
  7.  
  8. unknown = 'u'
  9. empty = 'e'
  10. full = 'f'
  11.  
  12. duckClues = (duckRows, duckCols) :: Clues
  13. poundClues = (poundRows, poundCols) :: Clues
  14.  
  15. duckRows = [[3], [5], [4,3], [7], [5], [3], [5], [1,8],
  16. [3,3,3], [7,3,2], [5,4,2], [8,2], [10], [2,3], [6]]
  17.  
  18. duckCols = [[3], [4], [5], [4], [5], [6], [3,2,1], [2,2,5],
  19. [4,2,6], [8,2,3], [8,2,1,1], [2,6,2,1], [4,6], [2,4], [1]]
  20.  
  21.  
  22. poundRows = [[4], [2,1], [1,2], [2,2], [2], [8], [2], [8],
  23. [2], [2], [2,2,2], [6,3], [2,5,3], [2,2,6], [4,4]]
  24.  
  25. poundCols = [[2],[4],[2,1],[1,1,2,1],[1,1,4],[11],[12],
  26. [2,1,1,2],[1,1,1,3],[1,1,1,2],[1,1,1,2],[3,3],[2,3],[3],[2]]
  27.  
  28. yinYangClues :: Clues
  29. yinYangClues = (yinYangRows, yinYangCols)
  30.  
  31. yinYangRows = [[8],[4,4],[2,6],[1,3,2],[3,3],[8],[6],[2,5],
  32. [1,2,4],[2,5],[4,5],[8]]
  33. yinYangCols = [[4,4],[3,3],[2,2],[2,2,2],[1,3,2,1],[1,4,2],
  34. [7,3],[3,7],[2,6],[10],[8],[4]]
  35.  
  36. flowerClues :: Clues
  37. flowerClues = (flowerRows, flowerCols)
  38.  
  39. flowerRows = [[2,2],[1,1,1],[1,1,1],[1,5,2],[1,2,2,1],[1,5,1],
  40. [2,1,3],[2,2],[1],[2,1,2],[3],[1]]
  41. flowerCols = [[2],[1,1],[2,1],[1,3,1,1],[1,3,1,1],[3,2,1],[1,3,5],
  42. [1,3,1,1],[2,1,1],[1,1,1],[1,1],[2]]
  43.  
  44. showCell c
  45. | c == unknown = '?'
  46. | c == empty = ' '
  47. | c == full = '#'
  48.  
  49. showRow xs = "|" ++ [showCell a|a<-xs] ++ "|\n"
  50.  
  51. showTable xss = putStrLn(concat [showRow xs|xs<-xss])
  52.  
  53. empties n = [empty|a<-[1..n]]
  54.  
  55. fulls n = [full|a<-[1..n]]
  56.  
  57. placeOneBlock x n = [(empties i) ++ (fulls x) ++ empties (n-i-x)|i<-[0..(n-x)]]
  58.  
  59. emptyLineOptions [] n
  60. | n > 0 = [empties n]
  61. | otherwise = [[]]
  62.  
  63. emptyLineOptions (x:xs) n
  64. | (length xs) == 0 = placeOneBlock x n
  65. | (length xs) > 0 && ((x+sum xs+sum [1|i<-xs])-1) > n = []
  66. | otherwise = [empties i ++ fulls x ++ empties 1 ++ j|i<-[0..(n-(x+sum xs+sum [1|i<-xs]-1))],j<-(emptyLineOptions xs (n-x-i-1))]
  67.  
  68. isMatching c1 c2
  69. |c1==full && c2/=full = False
  70. |c1==empty && c2/=empty = False
  71. |otherwise = True
  72.  
  73. lineOptions xs ys
  74. |null[y|y<-ys,y/=unknown] = emptyLineOptions xs (length ys)
  75. |otherwise = [x|x<-emptyLineOptions xs (length ys),length([i|i<-[0..(length(ys)-1)],isMatching (ys!!i) (x!!i)])==length ys]
  76.  
  77. combineOption c1 c2
  78. |c1==full && c2==full = full
  79. |c1==empty && c2==empty = empty
  80. |otherwise = unknown
  81.  
  82. combineLineOptions [xs] = xs
  83. combineLineOptions (xs:xss) = [combineOption (xs!!i) ((combineLineOptions xss)!!i)|i<-[0..((length xs)-1)]]
  84.  
  85. reduceLine xs ys
  86. |xs==[] = ys
  87. |otherwise = combineLineOptions (lineOptions xs ys)
  88.  
  89. reduceRows xss yss = [reduceLine (xss!!i) (yss!!i)|i<-[0..((length xss)-1)]]
  90.  
  91. reduceTable xss yss = List.transpose(reduceRows (snd xss) (List.transpose(reduceRows (fst xss) yss)))
  92.  
  93. emptyTable n m = [[unknown|i<-[1..m]]|j<-[1..n]]
  94.  
  95. recSolve xss yss
  96. | (reduceTable xss yss) /= yss = recSolve xss (reduceTable xss yss)
  97. | otherwise = yss
  98.  
  99. solve xss = recSolve xss (emptyTable (length (fst xss)) (length (snd xss)))
  100.  
  101. draw xss = showTable (solve xss)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement