summaryrefslogtreecommitdiff
path: root/SudokuOriginal.hs
blob: 2f03d5aba18c1f12b32cf54f193d131d56cfc88a (plain)
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
module SudokuOriginal where

import Data.List
import Control.Applicative
import SudokuHelper

numlist = [1..9]

pertake eve l cur [] = []
pertake eve l cur (x:xs) 
    | eve == cur = x:(pertake eve l 1 xs)
    | otherwise = if cur < l then x:rest else rest
        where rest = (pertake eve l (cur+1) xs)

block pos all = take 9 $ pertake 9 3 0 $ drop (x*9+y) all
    where x = i - mod i 3
          y = j - mod j 3
          i = div pos 9
          j = mod pos 9

line pos all = take 9 $ drop (pos - (mod pos 9)) all

col pos all = pertake 9 1 0 $ drop (mod pos 9) all

get pos all = (map head).group.sort.concat $ 
    ([block, line, col] <*> [pos]) <*> [all]

solve :: Int -> [Int] -> [[Int]]
solve 81 all = [all]
solve pos all = if head (drop pos all) == 0 then concatMap (solve (pos+1)) 
    [ take pos all ++ (i:(drop (pos+1) all)) | i<-avail ]
                            else solve (pos+1) all
    where avail = [i |i<-numlist, not $ elem i $ get pos all]

solveStr =  head . (solve 0)