Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created December 3, 2013 04:43
Show Gist options
  • Select an option

  • Save yuroyoro/7763966 to your computer and use it in GitHub Desktop.

Select an option

Save yuroyoro/7763966 to your computer and use it in GitHub Desktop.
社長令嬢の新人女子プログラマ野田さんにHaskell教えてみた 新人女子プログラマの書いたコードを直すだけの簡単なお仕事です!|paizaオンラインハッカソンVol.1 https://paiza.jp/poh/ec-campaign Arrowとか使えばもっときれいに書けそうな気がするがここが限界です……
{-
新人女子プログラマの書いたコードを直すだけの簡単なお仕事です!|paizaオンラインハッカソンVol.1 https://paiza.jp/poh/ec-campaign
-}
import Data.List
import Data.Maybe
main = do cs <- getContents
putStr $ unlines $ (map show) $ parse $ lines cs
parse :: [String] -> [Int]
parse ls = map (findBestPrice prices) goals
where
(item, days, xs) = parseHeader ls
(prices, ys) = takeNLines item xs
(goals, _) = takeNLines days ys
parseHeader :: [String] -> (Int, Int, [String])
parseHeader ls = (read item, read days, xs)
where
(x:xs) = ls
(item:days:_) = words x
takeNLines :: Int -> [String] -> ([Int], [String])
takeNLines n ls = (map read gps, xs)
where
(gps, xs) = splitAt n ls
findBestPrice :: [Int] -> Int -> Int
findBestPrice ips gp = fromMaybe 0 $ listToMaybe $ reverse $ fst $ span (<= gp) $ sort $ (map sum (combinationsN 2 ips))
combinationsN :: Int -> [a] -> [[a]]
combinationsN n = filter ((== n) . length) . subsequences
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment