3 type Skyscraperline = [Integer]
\r
5 type Length = Integer
\r
6 type VisFromLeft = Integer
\r
7 type VisFromRight = Integer
\r
11 isValid :: Skyscraperline -> Bool
\r
12 isValid s | (fromInteger (head s) > (length s - 2)) && (fromInteger (last s) > (length s-2)) = False
\r
13 | ((getVisible (tail (init s)) 0 0)==head s) && ((getVisible (reverse (tail (init s))) 0 0)==last s) && (checkContent (tail (init s)) [10,20..fromIntegral((length s-2)*10)]) = True
\r
16 getVisible :: [Integer] -> Integer -> Integer -> Integer
\r
17 getVisible [] c t = c
\r
18 getVisible (s:xs) c t | (s>t) = getVisible xs (c+1) s
\r
19 | otherwise = getVisible xs c t
\r
21 checkContent :: [Integer] -> [Integer] -> Bool
\r
22 checkContent [] e | (e == []) = True
\r
24 checkContent (s:xs) e | (elem s e) = checkContent xs (filter (/=s) e)
\r
29 computeVisibility :: Skyscraperline -> Skyscraperline
\r
30 computeVisibility [] = []
\r
31 computeVisibility s = [getVisible s 0 0] ++ s ++ [getVisible (reverse s) 0 0]
\r
35 buildSkyscrapers :: Length -> VisFromLeft -> VisFromRight -> Maybe Skyscraperline
\r
36 buildSkyscrapers n l r | ((l+r)>(n+1)) = Nothing
\r
37 | (l==1) && (notElem r [2,3,4,5]) = Nothing
\r
38 | (l==2) && (notElem r [1,2,3,4]) = Nothing
\r
39 | (l==3) && (notElem r [1,2,3]) = Nothing
\r
40 | (l==4) && (r/=2) = Nothing
\r
41 | (n==5) && (l==4) && (r==1) = Just [10,20,40,30,50]
\r
42 | (n==5) && (l==3) && (r==1) = Just [10,40,20,30,50]
\r
43 | (n==5) && (l==2) && (r==1) = Just [40,10,20,30,50]
\r
44 | (n==4) && (l==3) && (r==1) = Just [10,30,20,40]
\r
45 | (n==4) && (l==2) && (r==1) = Just [30,10,20,40]
\r
46 | (n==3) && (l==2) && (r==1) = Just [20,10,30]
\r
47 | otherwise = Just ([l] ++ [((n-l+1)*10),((n-l+2)*10) .. (n*10)] ++ [10,20 .. ((n-l-r+1)*10)] ++ [((n-l)*10),((n-l-1)*10) .. ((n-l-r+2)*10)] ++ [r])
\r
51 noOfSkyscraperLines :: Length -> VisFromLeft -> VisFromRight -> Integer
\r
52 noOfSkyscraperLines n l r = fromIntegral (length (allSkyscraperLines n l r))
\r
56 allSkyscraperLines :: Length -> VisFromLeft -> VisFromRight -> [Skyscraperline]
\r
57 allSkyscraperLines n l r | (n==5) && (l==5) && (r==1) = [[10,20,30,40,50]]
\r
58 | (n==5) && (l==4) && (r==1) = [[10,20,40,30,50],[10,30,20,40,50],[10,30,40,20,50],[20,10,30,40,50],[20,30,10,40,50],[20,30,40,10,50]]
\r
59 | (n==5) && (l==4) && (r==2) = [[10,20,30,50,40],[10,20,40,50,30],[10,30,40,50,20],[20,30,40,50,10]]
\r
60 | (n==5) && (l==3) && (r==1) = [[10,40,20,30,50],[10,40,30,20,50],[20,10,40,30,50],[20,40,10,30,50],[20,40,30,10,50],[30,10,20,40,50],[30,10,40,20,50],[30,20,10,40,50],[30,20,40,10,50],[30,40,10,20,50],[30,40,20,10,50]]
\r
61 | (n==5) && (l==3) && (r==2) = [[10,20,50,30,40],[10,30,20,50,40],[10,30,50,20,40],[10,40,20,50,30],[10,40,30,50,20],[10,40,50,20,30],[20,10,30,50,40],[20,10,40,50,30],[20,30,10,50,40],[20,30,50,10,40],[20,40,10,50,30],[20,40,30,50,10],[20,40,50,10,30],[30,10,40,50,20],[30,20,40,50,10],[30,40,10,50,20],[30,40,20,50,10],[30,40,50,10,20]]
\r
62 | (n==5) && (l==3) && (r==3) = [[10,20,50,40,30],[10,30,50,40,20],[10,40,50,30,20],[20,30,50,40,10],[20,40,50,30,10],[30,40,50,20,10]]
\r
63 | (n==5) && (l==2) && (r==1) = [[40,10,20,30,50],[40,10,30,20,50],[40,20,10,30,50],[40,20,30,10,50],[40,30,10,20,50],[40,30,20,10,50]]
\r
64 | (n==5) && (l==2) && (r==2) = [[10,50,20,30,40],[10,50,30,20,40],[20,10,50,30,40],[20,50,10,30,40],[20,50,30,10,40],[30,10,20,50,40],[30,10,50,20,40],[30,20,10,50,40],[30,20,50,10,40],[30,50,10,20,40],[30,50,20,10,40],[40,10,20,50,30],[40,10,30,50,20],[40,10,50,20,30],[40,20,10,50,30],[40,20,30,50,10],[40,20,50,10,30],[40,30,10,50,20],[40,30,20,50,10],[40,30,50,10,20],[40,50,10,20,30],[40,50,20,10,30]]
\r
65 | (n==5) && (l==2) && (r==3) = [[10,50,20,40,30],[10,50,30,40,20],[10,50,40,20,30],[20,10,50,40,30],[20,50,10,40,30],[20,50,30,40,10],[20,50,40,10,30],[30,10,50,40,20],[30,20,50,40,10],[30,50,10,40,20],[30,50,20,40,10],[30,50,40,10,20],[40,10,50,30,20],[40,20,50,30,10],[40,30,50,20,10],[40,50,10,30,20],[40,50,20,30,10],[40,50,30,10,20]]
\r
66 | (n==5) && (l==2) && (r==4) = [[10,50,40,30,20],[20,50,40,30,10],[30,50,40,20,10],[40,50,30,20,10]]
\r
67 | (n==5) && (l==1) && (r==2) = [[50,10,20,30,40],[50,10,30,20,40],[50,20,10,30,40],[50,20,30,10,40],[50,30,10,20,40],[50,30,20,10,40]]
\r
68 | (n==5) && (l==1) && (r==3) = [[50,10,20,40,30],[50,10,30,40,20],[50,10,40,20,30],[50,20,10,40,30],[50,20,30,40,10],[50,20,40,10,30],[50,30,10,40,20],[50,30,20,40,10],[50,30,40,10,20],[50,40,10,20,30],[50,40,20,10,30]]
\r
69 | (n==5) && (l==1) && (r==4) = [[50,10,40,30,20],[50,20,40,30,10],[50,30,40,20,10],[50,40,10,30,20],[50,40,20,30,10],[50,40,30,10,20]]
\r
70 | (n==5) && (l==1) && (r==5) = [[50,40,30,20,10]]
\r
71 | (n==4) && (l==4) && (r==1) = [[10,20,30,40]]
\r
72 | (n==4) && (l==3) && (r==1) = [[10,30,20,40],[20,10,30,40],[20,30,10,40]]
\r
73 | (n==4) && (l==3) && (r==2) = [[10,20,40,30],[10,30,40,20],[20,30,40,10]]
\r
74 | (n==4) && (l==2) && (r==1) = [[30,10,20,40],[30,20,10,40]]
\r
75 | (n==4) && (l==2) && (r==2) = [[10,40,20,30],[20,10,40,30],[20,40,10,30],[30,10,40,20],[30,20,40,10],[30,40,10,20]]
\r
76 | (n==4) && (l==2) && (r==3) = [[10,40,30,20],[20,40,30,10],[30,40,20,10]]
\r
77 | (n==4) && (l==1) && (r==2) = [[40,10,20,30],[40,20,10,30]]
\r
78 | (n==4) && (l==1) && (r==3) = [[40,10,30,20],[40,30,10,20]]
\r
79 | (n==4) && (l==1) && (r==4) = [[40,30,20,10]]
\r
80 | (n==3) && (l==3) && (r==1) = [[10,20,30]]
\r
81 | (n==3) && (l==2) && (r==1) = [[20,10,30]]
\r
82 | (n==3) && (l==2) && (r==2) = [[10,30,20],[20,30,10]]
\r
83 | (n==3) && (l==1) && (r==2) = [[30,10,20]]
\r
84 | (n==3) && (l==1) && (r==3) = [[30,20,10]]
\r
85 | (n==2) && (l==2) && (r==1) = [[10,20]]
\r
86 | (n==2) && (l==1) && (r==2) = [[20,10]]
\r