Skip to content

Instantly share code, notes, and snippets.

@jkramer jkramer/initphase.hs
Created Jan 12, 2016

Embed
What would you like to do?
CodinGame - APU: Init Phase
import Control.Monad
import Data.List
main = do
[ _, height ] <- replicateM 2 (fmap read getLine)
cells <- fmap concat (forM [0 .. height - 1] (\ y -> fmap (parseRow y) getLine))
forM cells (putStrLn . findNext cells)
where
parseRow y cells = map ((,) y . fst) $ filter ((==) '0' . snd) $ zip [0..] cells
findNext cells cell@(y, x) =
unwords (map showCell [cell, fallback right, fallback below])
where
right = filter (\ (y', x') -> y == y' && x' > x) cells
below = filter (\ (y', x') -> x == x' && y' > y) cells
fallback [] = (-1, -1)
fallback (cell : _) = cell
showCell (y, x) = show x ++ " " ++ show y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.