Skip to content

Instantly share code, notes, and snippets.

@joachifm
Created May 6, 2011 07:23
Show Gist options
  • Save joachifm/958558 to your computer and use it in GitHub Desktop.
Save joachifm/958558 to your computer and use it in GitHub Desktop.
Problem A
import Control.Monad
import Data.List (find, unfoldr)
main = interact (unlines . map (uncurry fmt) . zip [1..] . map solve . parse)
where fmt n (Just (x, y)) = "Case #" ++ show n ++ ": " ++ unwords [show x, show y]
fmt _ Nothing = ""
solve (c, xs) = go $ zip [1..] xs
where go ((i, n):ys) =
case find (\(_, n') -> n' + n == c) ys of
Just (i', _) -> Just (i, i')
Nothing -> go ys
go [] = Nothing
parse = unfoldr g . tail . lines
where g (a:_:b:xs) = return ((read a, map read $ words b), xs)
g _ = mzero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment