import System.IO
import Data.List
import Crossword
import CrosswordSample
fname = "p7_09d.dat"
-- myGrid = grid1
-- myWords = words1
-- firstPass = sinkSites ([], allSites)
-- showSolution = render 1 $ toLinear $ head $ triage 15 firstPass
-- (list of words, list of grid lines)
type Unsolved = ([String], [String])
solveFile :: IO ()
solveFile = do
handle <- openFile fname ReadMode
contents <- hGetContents handle
let split@(myWords, myGrid) = splitWords contents
-- putStrLn . show $ split
let allSites = getAllSites split
let firstPass = sinkSites ([], allSites)
render 1 $ toLinear $ head $ triage 1 firstPass
hClose handle
-- Split the file into array of words and grid lines
splitWords :: String -> Unsolved
splitWords s = groups
where
ls = lines s
dot a b = (elem '.' a) && (elem '.' b)
place x (words, dots)
| elem '.' x = (words, x:dots)
| (length x) > 0 = (x:words, dots)
| otherwise = (words, dots)
groups = foldr place ([], []) ls
-- Get all the horizontal and vertical sites from an Unsolved set
getAllSites :: Unsolved -> [Site]
getAllSites (words, grid) = hsites ++ vsites
where
hsites = getSites False words (makeGrid False grid)
vsites = getSites True words (makeGrid True $ rotateLeft grid)