Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- type Cell = Char
- type Table = [[Cell]]
- type ClueLine = [Int]
- type Clues = ([ClueLine], [ClueLine])
- unknown = 'u'
- empty = 'e'
- full = 'f'
- duckClues = (duckRows, duckCols) :: Clues
- poundClues = (poundRows, poundCols) :: Clues
- duckRows = [[3], [5], [4,3], [7], [5], [3], [5], [1,8],
- [3,3,3], [7,3,2], [5,4,2], [8,2], [10], [2,3], [6]]
- duckCols = [[3], [4], [5], [4], [5], [6], [3,2,1], [2,2,5],
- [4,2,6], [8,2,3], [8,2,1,1], [2,6,2,1], [4,6], [2,4], [1]]
- poundRows = [[4], [2,1], [1,2], [2,2], [2], [8], [2], [8],
- [2], [2], [2,2,2], [6,3], [2,5,3], [2,2,6], [4,4]]
- poundCols = [[2],[4],[2,1],[1,1,2,1],[1,1,4],[11],[12],
- [2,1,1,2],[1,1,1,3],[1,1,1,2],[1,1,1,2],[3,3],[2,3],[3],[2]]
- yinYangClues :: Clues
- yinYangClues = (yinYangRows, yinYangCols)
- yinYangRows = [[8],[4,4],[2,6],[1,3,2],[3,3],[8],[6],[2,5],
- [1,2,4],[2,5],[4,5],[8]]
- yinYangCols = [[4,4],[3,3],[2,2],[2,2,2],[1,3,2,1],[1,4,2],
- [7,3],[3,7],[2,6],[10],[8],[4]]
- flowerClues :: Clues
- flowerClues = (flowerRows, flowerCols)
- flowerRows = [[2,2],[1,1,1],[1,1,1],[1,5,2],[1,2,2,1],[1,5,1],
- [2,1,3],[2,2],[1],[2,1,2],[3],[1]]
- flowerCols = [[2],[1,1],[2,1],[1,3,1,1],[1,3,1,1],[3,2,1],[1,3,5],
- [1,3,1,1],[2,1,1],[1,1,1],[1,1],[2]]
- showCell :: Cell -> Char
- showCell c
- | c == unknown = '?'
- | c == empty = ' '
- | c == full = '#'
- showRow :: [Cell] -> String
- showRow r = "|"++(map showCell r)++"|\n"
- showTable :: Table -> String
- showTable t = concat $ map showRow t
- empties :: Int -> [Cell]
- empties n = replicate n 'e'
- fulls :: Int -> [Cell]
- fulls n = replicate n 'f'
- placeOneBlock :: Int -> Int -> [[Cell]]
- placeOneBlock x n = [ (empties i) ++ (fulls x) ++ (empties (n - x - i)) | i <- [0..n - x]]
- emptyLineOptions :: ClueLine -> Int -> [[Cell]]
- emptyLineOptions [] 0 = [[]]
- emptyLineOptions [] e = empties e:[]
- emptyLineOptions (c:[]) e = placeOneBlock c e
- emptyLineOptions (c:rest) e
- | e < c = []
- | True = reverse ([empty : row | row <- reverse (emptyLineOptions (c:rest) (e-1))] ++
- if null rest then [replicate c full ++ replicate (e-c) empty]
- else [replicate c full ++ empty : row | row <- reverse (emptyLineOptions rest(e-c-1))])
- isMatching :: Cell -> Cell -> Bool
- isMatching 'f' 'e' = False
- isMatching 'e' 'f' = False
- isMatching _ _ = True
- lineOptions :: ClueLine -> [Cell] -> [[Cell]]
- lineOptions clues mask = [option | option <- emptyLineOptions clues (length mask), and $ zipWith isMatching option mask]
- combineOption :: Cell -> Cell -> Cell
- combineOption 'f' 'f' = full
- combineOption 'e' 'e' = empty
- combineOption _ _ = unknown
- combineLineOptions :: [[Cell]] -> [Cell]
- combineLineOptions (o:[]) = o
- combineLineOptions (o:rst) = zipWith combineOption o (combineLineOptions rst)
- reduceLine :: ClueLine -> [Cell] -> [Cell]
- reduceLine [] mask = mask
- reduceLine clueline mask = combineLineOptions $ lineOptions clueline mask
- reduceRows :: [ClueLine] -> Table -> Table
- reduceRows cluelines table = zipWith reduceLine cluelines table
- emptyTable :: Int -> Int -> Table
- emptyTable n m = [replicate m unknown | n <- [1..n]]
- reduceTable :: Clues -> Table -> Table
- reduceTable (rowClues, colClues) table = transpose $ reduceRows colClues (transpose (reduceRows rowClues table))
- recSolve :: Clues -> Table -> Table
- recSolve ([],[]) table = table
- recSolve clues table
- | table == next = next
- | table /= next = recSolve clues next
- where next = [[]] -- itt nem tudom, hogyan kellene folytatni
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement