Skip to content

Instantly share code, notes, and snippets.

@petermarks
Created December 20, 2012 19:51
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 petermarks/4348043 to your computer and use it in GitHub Desktop.
Save petermarks/4348043 to your computer and use it in GitHub Desktop.
Room allocations
module Alloc where
import Data.List
import Control.Arrow
data Allocs = Allocs {checkouts :: [Int], checkins :: [Int]} deriving (Eq, Show)
allocs :: [[Int]] -> (Int, [Allocs])
allocs = first length . mapAccumL f []
where
f :: [Int] -> [Int] -> ([Int], Allocs)
f rooms arrivals = (rooms'', Allocs outs ins)
where
rooms' = map (subtract 1) rooms
outs = [room | (nights, room) <- zip rooms' [0..], nights == 0]
free = [room | (nights, room) <- zip rooms' [0..], nights <= 0]
ins = [room | (nights, room) <- zip arrivals (free ++ [length rooms..])]
rooms'' = allocNewRooms rooms' arrivals
allocNewRooms :: [Int] -> [Int]-> [Int]
allocNewRooms [] guests = guests
allocNewRooms rooms [] = rooms
allocNewRooms (r:rooms) (g:guests) | r <= 0 = g : allocNewRooms rooms guests
allocNewRooms (r:rooms) guests = r : allocNewRooms rooms guests
module Allocation where
import Data.List
import Control.Arrow
data Actions = Actions {deallocs :: [Int], allocs :: [Int]}
deriving (Eq, Show)
actions :: [[Int]] -> (Int, [Actions])
actions = first (\(_,_,maxSlot) -> maxSlot + 1) . mapAccumL processStep ([0..], repeat [], 0)
where
processStep (free, ds : futureDeallocs, maxSlot) items = (state', Actions ds as)
where
(state', as) = mapAccumL processItem (ds ++ free, futureDeallocs, maxSlot) items
processItem (slot : free', futureDeallocs, maxSlot) item =
((free', modifyNth (slot:) (item-1) futureDeallocs, max slot maxSlot), slot)
modifyNth :: (a -> a) -> Int -> [a] -> [a]
modifyNth _ _ [] = []
modifyNth f 0 (x : xs) = f x : xs
modifyNth f n (x : xs) = x : modifyNth f (n-1) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment