-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyulhap.hs
37 lines (30 loc) · 1.21 KB
/
gyulhap.hs
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
36
37
import Data.List (nub, intercalate)
import Data.Maybe (mapMaybe)
import Board
main = do
contents <- getContents
let inputBoard :: Board
inputBoard = map read $ filter (not . null) $ lines contents
solutionHaps = findHaps inputBoard
putStrLn $ serveOutputText solutionHaps
findHaps :: Board -> [Hap]
findHaps [] = []
findHaps [_] = []
findHaps (a:xs) = findHaps' a xs ++ findHaps xs
where findHaps' :: Tile -> Board -> [Hap]
findHaps' _ [] = []
findHaps' t (b:xs) = mapMaybe (makeHap t b) xs ++ findHaps' t xs
makeHap :: Tile -> Tile -> Tile -> Maybe Hap
makeHap t1 t2 t3 = let tiles = [t1, t2, t3]
in if isHap tiles then Just $ Hap t1 t2 t3
else Nothing
isHap :: [Tile] -> Bool
isHap tiles = allSameOrDifferent colour tiles
&& allSameOrDifferent shape tiles
&& allSameOrDifferent background tiles
allSameOrDifferent :: (Eq a) => (Tile -> a) -> [Tile] -> Bool
allSameOrDifferent f es = let numDistincts = length $ nub (map f es)
in numDistincts == 1 || numDistincts == length es
serveOutputText :: [Hap] -> String
serveOutputText [] = "\nThere are zero haps on this board!"
serveOutputText haps = intercalate "\n\n" $ "\nThe haps are:" : map show haps