Created
October 1, 2012 14:07
-
-
Save thomwiggers/3811993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
- Thom Wiggers | |
- s4119444 | |
- | |
- Dit is een ietwat specifieke versie die | |
- voor 4x4 matrixes is gemaakt. Dit is redelijk makkelijk | |
- te veranderen, maar dan moet er een boel (length woordzoeker) bij | |
- en ik had daar geen zin meer in. | |
-} | |
import Data.List | |
import Data.Maybe -- hey, I Just met you, and this is crazy, | |
-- so here's my Maybe Int, so call me maybe | |
main = putStrLn (solveWoordzoeker woordzoeker woordenlijst) | |
-- | |
-- Haskell heeft strings als [Char] gedefinieerd. Dat scheelt een hoop. | |
-- | |
woordzoeker :: [String] | |
woordzoeker = ["TERP", | |
"CWLF", | |
"LAEE", | |
"ALNE"] | |
-- woordenlijst met woorden om te verwijderen | |
woordenlijst :: [String] | |
woordenlijst = ["FEL", "TWEE", "PRET", "ALP" ] | |
-- | |
-- Lijst met coordinaten van letters | |
-- Wordt gebruikt om letters uit weg te strepen | |
-- | |
letterCoordinaten :: [String] -> [(Int,Int)] | |
letterCoordinaten woordzoeker = | |
[ (a,b) | a <- [0..(l)], b <- [0..(l)] ] | |
where l = (length woordzoeker) - 1 | |
-- | |
-- Haalt een bepaalde letter uit de woordzoekermatrix | |
-- | |
getLetter :: [String] -> (Int,Int) -> Char | |
getLetter wz (a,b) = wz !! a !! b | |
-- | |
-- geeft van een matrix alleen de letters op de gegeven coordinaten. | |
-- | |
printWoord :: [String] -> [(Int,Int)] -> String | |
printWoord woordzoeker coordinaten = [ woordzoeker !! a !! b | (a,b) <- coordinaten] | |
-- | |
-- rijContains | |
-- Returnt true als woord in een row zit | |
-- | |
rijContains :: String -> String -> Bool | |
rijContains row word = | |
word `isInfixOf` row || rword `isInfixOf` row | |
where rword = reverse word | |
-- | |
-- haalt de coordinaten op van de letters van een woord | |
-- | |
getCoordinaten :: String -> [String] -> String -> [(Int,Int)] | |
getCoordinaten row matrix word | |
= getCoordinatenD row a word [] ++ getCoordinatenD row a (reverse word) [] | |
where a = fromJust (row `elemIndex` matrix) | |
-- | |
-- accumulatorversie van getCoordinaten | |
-- | |
getCoordinatenD :: String -> Int -> String -> [(Int,Int)] -> [(Int,Int)] | |
getCoordinatenD _ _ [] a = a | |
getCoordinatenD [] _ _ _ = [] -- blijkbaar klopt er iets niet/geen volledige match | |
getCoordinatenD (r:row) a (w:word) accum | |
| r == w = getCoordinatenD row a word newAccum | |
| r /= w = getCoordinatenD row a (w:word) accum | |
where newAccum = ((a, ((length woordzoeker) - (length row+1))):accum) | |
-- | |
-- kolomContains | |
-- returnt true als woord in kolom n zit | |
-- | |
kolomContains :: [String] -> Int -> String -> Bool | |
kolomContains matrix n woord = rijContains ((transpose matrix) !! n ) woord | |
-- | |
-- Los een woordzoeker horizontaal op | |
-- | |
solveWoordzoekerHorizontaal :: [String] -> [String] -> [(Int,Int)] | |
solveWoordzoekerHorizontaal woordzoeker woordenlijst | |
= concat [ (getCoordinaten rij woordzoeker woord) | | |
rij <- woordzoeker, woord <- woordenlijst, rij `rijContains` woord] | |
-- | |
-- verticale aanpak | |
-- | |
solveWoordzoekerVerticaal :: [String] -> [String] -> [(Int,Int)] | |
solveWoordzoekerVerticaal woordzoeker woordenlijst | |
= [ (b,a) | (a,b) <- (concat [(getCoordinaten kolom (transpose woordzoeker) woord) | | |
kolom <- (transpose woordzoeker), woord <- woordenlijst, | |
kolom `rijContains` woord]) | |
] | |
-- | |
-- produceer lijsten met coordinaten, die samen een reeks letters vormen die | |
-- de schuine woorden representeren. | |
-- | |
schuinReeksen :: [(Int,Int)] | |
schuinReeksen = [(p+c,q+c) |(p,q)<- (startsL), c<-[0..3], p+c < 4, q+c < 4] ++ --richting ZO | |
[(p+c,q-c) |(p,q)<- (startsR), c<-[0..3], p+c < 4, q-c > -1] -- richting ZW | |
where startsL = (0,0):[(a,0) | a <- [1..3]] ++ [(0,a) | a<-[1..3]] ++ [(a,3) | a<-[0..3] ] | |
startsR = [(0,a) | a<-[1..3]] ++ [(a,3) | a<-[1..2]] | |
-- | |
-- maak een lijst met nummers die horen bij een woord | |
-- | |
getNumbers :: String -> String -> [Int] | |
getNumbers reeksen woord = (getNumbersD reeksen woord [] 0) ++ | |
(getNumbersD reeksen (reverse woord) [] 0) | |
-- | |
-- accumulator versie | |
-- | |
getNumbersD :: String -> String -> [Int] -> Int -> [Int] | |
getNumbersD "" _ _ _ = [] | |
getNumbersD _ "" lijst _ = lijst | |
getNumbersD reeksen woord lijst offset | |
| woord `isPrefixOf` reeksen = getNumbersD (tail reeksen) (tail woord) (lijst ++ [offset]) (offset+1) | |
| woord `isInfixOf` reeksen = getNumbersD (tail reeksen) woord [] (offset+1) | |
| otherwise = [] | |
-- | |
-- Schuin matchen | |
-- TODO | |
-- | |
solveWoordzoekerSchuin :: [String] -> [String] -> [(Int,Int)] | |
solveWoordzoekerSchuin woordzoeker woordenlijst = [sr !! a | woord <- woordenlijst, a <- | |
(getNumbers (printWoord woordzoeker sr) woord)] | |
where sr = schuinReeksen | |
-- | |
-- Probeer de woordzoeker op te lossen | |
-- | |
solveWoordzoeker :: [String] -> [String] -> String | |
solveWoordzoeker woordzoeker woordenlijst = | |
printWoord woordzoeker | |
((letterCoordinaten woordzoeker) \\ ((solveWoordzoekerHorizontaal woordzoeker woordenlijst ) ++ | |
(solveWoordzoekerVerticaal woordzoeker woordenlijst ) ++ | |
( solveWoordzoekerSchuin woordzoeker woordenlijst ))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment