Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. > module Sudokus where
  2.  
  3.  
  4. > type Sudoku = [Integer]
  5.  
  6. if there are no more 0s then we are all done!
  7.  
  8. > solved s = 0 `notElem` s
  9.  
  10. Replace the 0 by the move you are making
  11.  
  12.  
  13. > choose s (cell, mov) = zipWith choose' s [0..]
  14. >   where
  15. >     choose' digit index | cell == index = mov
  16. >                         | otherwise     = digit
  17.  
  18. > choices s = zip (repeat zeroCells) (posDigits s zeroCells)
  19. >   where
  20. >     zeroCells = case filter isZero (zip s [0..]) !! 0 of
  21. >                   (0, index) -> index
  22. >       where
  23. >         isZero (digit, index) = digit == 0
  24.  
  25. these need to probably be edited for squig:
  26.  
  27. posDigits are the digits which are not in a cell already
  28.  
  29. > posDigits s cell = [digit | digit <- [1..9], free digit]
  30. >   where
  31. >     free digit = digit `notElem` [s !! x | x <- indices cell]
  32.  
  33. > indices n = col n ++ row n ++ box n
  34.  
  35. > col     n = take 8 [x   | x <- [n `mod` 9, n `mod` 9 + 9 ..], x   /= n]
  36. > row     n = take 8 [x   | x <- [n - n `mod` 9 ..],            x   /= n]
  37. > --box     n =        [x+y | x <- rows, y <- cols,               x+y /= n]
  38. > box     n = findBoxIndexes n makeSquigelyBoxes
  39. >   where
  40. >     rows = take 3 [n - n `mod` 27, n - n `mod` 27 + 9 ..]
  41. >     cols = take 3 [n `mod` 9 - n `mod` 3 ..]
  42.  
  43. > squ1 = [0,1,2,9,11,18,20,27,29]
  44. > squ2 = map (+3) squ1
  45. > squ3 = map (+3) squ2
  46. > squ4 = [10,19,28,36,37,38,46,55,64]
  47. > squ5 = map (+3) squ4
  48. > squ6 = map (+3) squ5
  49. > squ7 = [45,47,54,56,63,65,72,73,74]
  50. > squ8 = map (+3) squ7
  51. > squ9 = map (+3) squ8
  52.  
  53. > makeSquigelyBoxes = squ1 : squ2 : squ3 : squ4 : squ5 : squ6 : squ7 : squ8 : squ9 : []
  54.  
  55. > findBoxIndexes _ [] = error "There was a problem"
  56. > findBoxIndexes a xs = filter (a /= ) $ head $ filter (a `elem`) xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement