-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeatMap.hs
45 lines (37 loc) · 1.14 KB
/
SeatMap.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
38
39
40
41
42
43
44
45
module SeatMap
( SeatPos,
SeatMap,
SeatState (Floor, Empty, Occupied),
Seat,
getSeatMap,
printSeatMap) where
import qualified Data.Map as Map
recSplitAt::Int -> [t] -> [[t]]
recSplitAt _ [] = []
recSplitAt i l =
let (h, rest) = splitAt i l
in h : recSplitAt i rest
printSeatMap:: Int -> SeatMap -> IO ()
printSeatMap lineWidth seatMap = do
putStrLn $ unlines $ map (\x -> foldl1 (++) (show <$> x)) (recSplitAt lineWidth $ Map.elems seatMap)
return ()
getSeatMap::[String] -> SeatMap
getSeatMap input =
Map.fromList $ concat $ zipWith getSeats input [0..]
getSeats:: [Char] -> Int -> [Seat]
getSeats row rowNumber =
zip [[rowNumber, x] | x <- [0..(length row)]] (map (\x -> read [x]) row)
type SeatMap = Map.Map SeatPos SeatState
type Seat = (SeatPos, SeatState)
type SeatPos = [Int]
data SeatState = Floor | Empty | Occupied deriving (Eq)
instance Show SeatState where
show Floor = "."
show Empty = "L"
show Occupied = "#"
instance Read SeatState where
readsPrec _ (x:xs) =
let a = case x of '.' -> Floor
'L' -> Empty
'#' -> Occupied
in [(a,xs)]