Created
September 7, 2012 20:50
-
-
Save joseanpg/3669514 to your computer and use it in GitHub Desktop.
reparte.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment | |
type Vec = [Int] -- (Int,Int,Int,Int,Int,Int) | |
data UnaSol = UnaSol { values :: [Vec], rest :: Vec} deriving (Show) | |
type Sol = [UnaSol] | |
beta :: Vec -> [Int] -> Sol | |
beta t [] = [UnaSol [] t] | |
beta [u,v,w,x,y,z] [h1] = [UnaSol [ [u1,v1,w1,x1,y1,z1] ] [u-u1,v-v1, w-w1, x-x1, y-y1, z-z1] | | |
u1<-[0..u], | |
v1<-[0..v], | |
w1<-[0..w], | |
x1<-[0..x], | |
y1<-[0..y], | |
z1<-[0..z], | |
u1+v1*2+w1*3+x1*4+y1*5+z1*6 == h1] | |
beta t (h:hs) = concatMap f (beta t [h]) | |
where { | |
f (UnaSol [x] r) = map g (beta r hs) | |
where { | |
g ( UnaSol xs r') = UnaSol (x:xs) r' | |
} | |
} | |
info :: Int->Int->IO() | |
info delta len = putStr ("Sobran " ++ (show delta) ++ "\n") >> | |
putStr ("Hay " ++ (show len) ++ " configuraciones posibles\n") | |
alpha :: Vec -> [Int] -> IO() | |
alpha t hs = let delta = sum (zipWith (*) t [1..6]) - (sum hs) | |
in if delta >= 0 then let result = beta t hs | |
len = length result | |
men = info delta len | |
in men >> mapM_ print (beta t hs) >> men | |
else putStr ("Faltan " ++ (show (-delta)) ++ "\n") | |
main :: IO () | |
main = do (disponible:demanda:_) <- getArgs | |
alpha (read disponible) (read demanda) | |
-- runhugs reparto_grupos.hs "[0,1,0,12,6,0]" "[12,16,16,18,18]" | |
-- runhugs reparto_grupos.hs "[2,3,4,9,6,2]" "[16,18,18,18,18,18]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment