Skip to content

Instantly share code, notes, and snippets.

@tolysz
Created December 21, 2012 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolysz/4350113 to your computer and use it in GitHub Desktop.
Save tolysz/4350113 to your computer and use it in GitHub Desktop.
A simple stream hotel room allocator...
module Hotel where
import Data.List
data Allocs = Allocs {checkouts :: [RoomNo], checkins :: [RoomNo], currentMax :: Int}
deriving (Eq)
instance Show Allocs where
show (Allocs xo xi cm) = "A" ++ (show xo) ++ " " ++ (show xi)++" " ++ (show cm) ++ " "
type GuestStay = Int
type RoomNo = Int
type DailyGuests = [ GuestStay ]
type Bookings = [ DailyGuests ]
type RoomUse = (RoomNo,GuestStay)
data Hotel = Hotel { inUse :: [RoomUse], ready :: [RoomUse] , virgin :: [RoomNo] } deriving Eq
instance Show Hotel where
show h = "<< " ++ ( show $ inUse h ) ++ " " ++ (show $ ready h ) ++ " " ++ (init $ (show (take 3 $ virgin h) )) ++ "...] >>"
tes :: Bookings
tes = [ [3,3], [2,1], [], [4], [2,2], [] ]
emptyH = Hotel [] [] [1..]
nextDay :: Hotel -> Hotel
nextDay (Hotel i r v) = Hotel (map (\(r,s)->(r,s - 1)) i) (map (\(r,s)->(r,s - 1)) r) v
allocs :: Bookings -> [Allocs]
allocs bs = worker bs emptyH
where
worker [] (Hotel [] _ _) = []
worker (b:bs) h = let (hh, aa) = f h b in aa : worker bs hh
worker [] h = let (hh, aa) = f h [] in aa : worker [] hh
f :: Hotel -> DailyGuests -> (Hotel, Allocs)
f h g = (Hotel i'' r'' v'', Allocs xo xi ((head v'')-1))
where
n = length g
(Hotel i r v) = nextDay h
i' = [ ru | ru@(_, stay) <- i, stay > 0 ]
r' = [ ru | ru@(_, stay) <- i, stay == 0]
xo = map fst r'
freeRooms = r ++ r'
freeRoomsNos = map fst freeRooms
r'' = drop n (freeRooms) -- replace by a better room reuse strategy
xi = take n (freeRoomsNos ++ v)
v'' = drop (n - (length freeRooms)) v
i'' = i' ++ zip xi g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment