Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created March 16, 2012 18:27
Show Gist options
  • Select an option

  • Save dradtke/2051679 to your computer and use it in GitHub Desktop.

Select an option

Save dradtke/2051679 to your computer and use it in GitHub Desktop.
Partial Haskell solution for Minimum Scalar Product
{-
- Problem URL: http://code.google.com/codejam/contest/32016/dashboard#s=p0
-
- solve takes as input a list of strings for a particular case
- and returns a string representation of its solution.
-}
solve :: [String] -> String
solve input = show $ minimum options
where (l1:l2:l3:_) = input
n = read l1 :: Int
v1 = parseVector l2 n
v2 = parseVector l3 n
pairs = [(a,b) | a <- permutations v1, b <- permutations v2]
options = map scalar pairs
parseVector :: String -> Int -> [Int]
parseVector line n = map read $ take n $ (words line) :: [Int]
scalar :: ([Int],[Int]) -> Int
scalar (v1,v2) = sum $ zipWith (*) v1 v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment