Advertisement
Prosoc

Griddler feladat

Nov 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.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.  
  45.  
  46.  
  47.  
  48. showCell :: Cell -> Char
  49. showCell c
  50.   | c == unknown = '?'
  51.   | c == empty = ' '
  52.   | c == full = '#'
  53.  
  54.  
  55. showRow :: [Cell] -> String
  56. showRow r =  "|"++(map showCell r)++"|\n"
  57.  
  58.  
  59. showTable :: Table -> String
  60. showTable t = concat $ map showRow t
  61.  
  62.  
  63. empties :: Int -> [Cell]
  64. empties n = replicate n 'e'
  65.  
  66.  
  67. fulls :: Int -> [Cell]
  68. fulls n = replicate n 'f'
  69.  
  70.  
  71. placeOneBlock :: Int -> Int -> [[Cell]]
  72. placeOneBlock x n = [ (empties i) ++ (fulls x) ++ (empties (n - x - i)) | i <- [0..n - x]]
  73.  
  74.  
  75. emptyLineOptions :: ClueLine -> Int -> [[Cell]]
  76. emptyLineOptions [] 0 = [[]]
  77. emptyLineOptions [] e = empties e:[]
  78. emptyLineOptions (c:[]) e = placeOneBlock c e
  79. emptyLineOptions (c:rest) e
  80.   | e < c = []
  81.   | True =  reverse ([empty : row | row <- reverse (emptyLineOptions (c:rest) (e-1))] ++
  82.     if null rest then [replicate c full ++ replicate (e-c) empty]
  83.                else [replicate c full ++ empty : row | row <- reverse (emptyLineOptions rest(e-c-1))])
  84.  
  85.  
  86. isMatching :: Cell -> Cell -> Bool
  87. isMatching 'f' 'e' = False
  88. isMatching 'e' 'f' = False
  89. isMatching _ _ = True
  90.  
  91.  
  92. lineOptions :: ClueLine -> [Cell] -> [[Cell]]
  93. lineOptions clues mask = [option | option <- emptyLineOptions clues (length mask), and $ zipWith isMatching option mask]
  94.  
  95.  
  96. combineOption :: Cell -> Cell -> Cell
  97. combineOption 'f' 'f' = full
  98. combineOption 'e' 'e' = empty
  99. combineOption  _ _ = unknown
  100.  
  101.  
  102. combineLineOptions :: [[Cell]] -> [Cell]
  103. combineLineOptions (o:[]) = o
  104. combineLineOptions (o:rst) = zipWith combineOption o (combineLineOptions rst)
  105.  
  106.  
  107. reduceLine :: ClueLine -> [Cell] -> [Cell]
  108. reduceLine [] mask = mask
  109. reduceLine clueline mask = combineLineOptions $ lineOptions clueline mask
  110.  
  111.  
  112. reduceRows :: [ClueLine] -> Table -> Table
  113. reduceRows cluelines table = zipWith reduceLine cluelines table
  114.  
  115. emptyTable :: Int -> Int -> Table
  116. emptyTable n m = [replicate m unknown | n <- [1..n]]
  117.  
  118.  
  119. reduceTable :: Clues -> Table -> Table
  120. reduceTable (rowClues, colClues) table = transpose $ reduceRows colClues (transpose (reduceRows rowClues table))
  121.  
  122.  
  123. recSolve :: Clues -> Table -> Table
  124. recSolve ([],[]) table = table
  125. recSolve clues table
  126.   | table == next = next
  127.   | table /= next = recSolve clues next
  128.   where next = [[]] -- itt nem tudom, hogyan kellene folytatni
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement