Print a single integer — the minimum number of moves needed to make the matrix beautiful.

import Control.Monad (replicateM)
import Data.List

main::IO()
main = do
    lst1 <- replicateM 5 getLine
    let lst = concatMap (filter (/=' ')) lst1
    let Just i = elemIndex '1' lst
    print $ abs (i `mod` 5 - 2) + abs (i `div` 5 - 2)

module Main
    where

main = do
    c <- coord
    print $ mini c
    where
        mini (x, y) =
            abs (3 - x) + abs (3 - y)
        coord =
            coord' 0 0
            where
                coord' 0 y = do
                    row <- getLine
                    coord' (getX row) (y + 1)
                    where
                        getX row =
                            case words row of
                                "1":_ -> 1
                                _:"1":_ -> 2
                                _:_:"1":_ -> 3
                                _:_:_:"1":_ -> 4
                                _:_:_:_:"1":_ -> 5
                                _ -> 0
                coord' x y =
                    return (x, y)

import Control.Monad
import Data.List
import Data.Maybe

removeSpace :: String -> String
removeSpace xs = [ x | x <- xs, not (x `elem` " ") ]

main :: IO ()
main = do
  inputs <- replicateM 5 getLine :: IO [[Char]]
  let ninputs = map removeSpace inputs
  let res = map (\x -> (elemIndex '1' x)) ninputs
  let resm = map (\x -> (fromMaybe (-1) x)) res
  let conc = zip resm [0..(length res)]
  let [(x, y)] = filter (\(a, _) -> a /= -1) conc
  print ((abs (2 - x)) + (abs (2 - y)))

import Control.Applicative

main = do
  ls <- lines <$> getContents
  let vec = concat $ map words ls
  let (a,b) = head $ [ (i,j) | i <- [0..4], j <- [0..4], (vec !! (i*5+j)) == "1"]
  print $ abs (2-a) + (abs (2-b))

import Control.Monad
import Data.List
import Data.Maybe

main :: IO ()
main = do
  a <- replicateM 5 $ fmap (elemIndex (1::Int) . map read . words) getLine
  let i = fromJust $ findIndex isJust a
      j = fromJust $ a!!i
  print $ abs (i - 2) + abs (j - 2)

findOne :: [Int] -> Int
findOne (a:b)
    | a == 1    = 0
    | otherwise = 1 + findOne b

main :: IO()
main = do
    str <- getContents
    let ind = findOne (map read (words str))
    (putStrLn . show) (abs ((mod ind 5) - 2) + abs ((div ind 5) - 2))

main = do
	al<-getContents
	putStrLn $ show $ solve $ map read $ concat $map (words) $ lines al

solve m = abs(3-i)+abs(3-j )
   where i = 1+div (one-1) 5
         j = 1+mod (one-1) 5
         one =1+ length (takeWhile (==0) m)

import Control.Applicative ((<$>))
import Data.List (elemIndex)
import Data.Maybe (fromJust)

main = lines <$> getContents >>= return . solve 0 0 . map words >>= print
       where solve a b (x:xs)
               | "1" `elem` x = (abs $ 2-a) + (abs $ 2 - (fromJust $ "1" `elemIndex` x))
               | otherwise    = solve (a+1) b xs

import Data.List (elemIndex)
import Data.Maybe (fromJust)

main = (sequence $ take 5 $ repeat getLine) >>= return . solve (0,0) . map words >>= print
       where solve (a,b) (x:xs)
               | "1" `elem` x = (abs $ 2-a) + (abs $ 2 - (fromJust $ "1" `elemIndex` x))
               | otherwise    = solve (a+1, b) xs

import Data.Char
import Data.List

f n (x:xs) = case elemIndex '1' x of
             Just m -> sum $ map (abs . subtract 2) [m, n]
             Nothing -> f (n + 1) xs

main = interact $ show . f 0 . map (filter $ not . isSpace) . lines

import Control.Monad
import Data.List
import Data.Maybe

-- Snippet: readItems
readItems = do
    line <- getLine
    return $ map read $ words line

main = do
    a <- replicateM 5 $ do
        ai <- readItems
        return $ elemIndex 1 ai
    let i1 = fromJust (findIndex isJust a)
    let i2 = fromJust (a !! i1)
    putStrLn $ show $ abs (i1 - 2) + abs (i2 - 2)

import Data.List ( elemIndex )
import Data.Maybe ( fromJust )

main :: IO ()
main = do
  vals <- sequence [getLine, getLine, getLine, getLine, getLine]
  print $ distance $ findOne (map words vals) 0

findOne :: Num t => [[[Char]]] -> t -> (t, Int)
findOne (x:xs) idx
  | elem "1" x = (idx, fromJust $ elemIndex "1" x)
  | otherwise  = findOne xs (idx + 1)

distance :: Num a => (a, a) -> a
distance (x, y) = abs(x - 2) + abs(y - 2)

main = do
    as <- fmap (map (map read . words) . lines) getContents

    let
        (i, j) = head [(i, j) | i <- [0..4], j <- [0..4], as!!i!!j == 1]

    print $ abs (2-i) + abs (2-j)

main = interact $ show . solve . map read . words

solve xs = abs (2 - y) + abs(2 - x)
    where   (k, _) : _ = filter (\(_,b) -> b == 1) . zip [0..] $ xs
            (x, y) = (mod k 5, div k 5)

import Data.List
f x = abs(a - 2) + abs(b - 2) where
	  Just g = findIndex (=="1") x
	  [a, b] = [div g 5, mod g 5]
main = interact $ show . f . words

import Data.List

(|>) x f = f x
main = interact process

process contents = let
  mat = contents
    |> lines
    |> map words
    |> map (map read) :: [[Integer]]
  in mat
    |> solve
    |> show

solve mat = solve' mat 0
  |> distance

distance (row, col) = abs (row - 2) + abs (col - 2)

solve' mat row = case findIndex (== 1) (mat !! row) of
  Nothing -> solve' mat (row + 1)
  (Just idx) -> (row, idx)

import Control.Monad
import qualified Data.List as L
import qualified Data.Maybe as M

main :: IO ()
main = do
    l <- (getContents >>= return . map read . words)
    putStrLn $ show $ solve l

solve :: [Int] -> Int
solve = calcMinStep . findPosition

findPosition :: [Int] -> (Int,Int)
findPosition l = (x `div` 5, x `mod` 5)
    where
        x = M.fromMaybe 0 (L.findIndex (==1) l)

calcMinStep :: (Int,Int) -> Int
calcMinStep (x,y) = abs (x-2) + abs(y-2)

{-# LANGUAGE BlockArguments #-}

import Control.Arrow
import Data.List

main = do { x <- solve 0; (show >>> putStrLn) x }

solve :: Int -> IO Int
solve a = do { s <- getLine
             ; let i = (words >>> map read >>> findIndex (==1)) s
               in case i of
                  Nothing -> solve (a+1)
                  Just v -> return $ (abs (2-v)) + (abs (2-a))
             }

import           Control.Applicative
import           Data.List

main = do
  x <- head . elemIndices 1 . tail . (0:) . map read . words <$> getContents
  print (abs (x `mod` 5 - 2) + abs (x `div` 5 - 2))

import Data.List
import Data.Maybe
main = readsolveprint
readsolveprint::IO()
readsolveprint =  interact $ show . solve. map  (map read. words).  take (5) . lines
solve :: [[Int]]->Int
solve xss = abs ((z`div`5)-2)+abs((z`mod`5)-2)
  where z= fromJust $ elemIndex 1 $ concat xss

import Control.Applicative
import Data.List

solve :: Maybe Int -> Int
solve (Just i) =
    let r = quot i 5
        c = rem i 5
    in abs (r - 2) + abs (c - 2)

main = do
    l <- (elemIndex '1'.filter (\x-> x `elem` "01")) <$> getContents
    print $ solve l

{-#LANGUAGE OverloadedStrings#-}
module Main where

import Data.Text (pack, unpack, splitOn, intercalate)

--confirm 5 row and five columns
-- find the position of the number
main :: IO ()
main = do
    strs <- getS
    let p = pos strs
    let ans = abs (2 - fst p) + abs (2 - snd p)
    putStrLn $ show ans

pos :: [String] -> (Int,Int)
pos s = (indexC s, maximum $ map indexR s)
        where
            indexC :: [String] -> Int
            indexC = go2 0
            go2 i [] = -1
            go2 i (c : cs)
                | '1' `elem` c = i
                | otherwise = go2 (i+1) cs
            indexR :: String -> Int
            indexR = go 0
            go i [] = -1
            go i (c : cs)
                | c == '1' = i
                | otherwise = go (i+1) cs

getS :: IO [String]
getS = sequence [getLine >>=
        (pure . unpack . intercalate ""
            . splitOn " " . pack) | _ <- [1..5] ]

import Data.List (findIndices)

solve :: String -> Int
solve  =  solve' . flip divMod 5 . head . findIndices (== "1") . words
  where
    solve' (i, j)  =  abs (i - 2) + abs (j - 2)

main :: IO ()
main  =  getContents >>= print . solve

-- Codeforces 263A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O3 #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Data.List

main :: IO ()
main = do
    mat <- replicateM 5 ((map read . words) <$> getLine) :: IO [[Int]]
    let Just row = findIndex (any (== 1)) mat
        Just column = findIndex (== 1) (mat !! row)
    print $ abs (row + 1 - 3) + abs (column + 1 - 3)

import Control.Applicative
import Control.Monad
import Data.Array (range)

dim = range ((0,0),(4,4)) :: [(Int,Int)]

parseInts = map read . words

main = do
  xs <- zip dim . concatMap parseInts <$> replicateM 5 getLine :: IO [((Int,Int),Int)]
  let ((i,j),_) = head $ filter ((/=0) . snd) xs
  print $ abs (i-2) + abs (j-2)

import Data.Function

main = findOne >>= putStrLn . show . calculateMoves

indexOf::[Char] -> Char -> Int
indexOf str c = fix (\fn len i res -> case () of
  _ | i >= len -> res
    | str !! i == c -> i
    | otherwise -> fn len (i + 2) res) (length str) 0 (-1) `div` 2

findOne::IO (Int, Int)
findOne = fix (\fn lineIndex -> do
  line <- getLine
  let columnIndex = indexOf line '1'
  if columnIndex == (-1)
  then fn (lineIndex + 1)
  else return (lineIndex, columnIndex)) 0

calculateMoves::(Int, Int) -> Int
calculateMoves coord = calculateDifference (fst coord)
  + calculateDifference (snd coord)

calculateDifference::Int -> Int
calculateDifference pos = abs (pos - 2)

import Text.Printf
import Data.List
import Data.Functor
import Control.Applicative
import Prelude
import Control.Monad
import Data.Maybe
import Data.Function

readInt :: String -> Int
readInt = read

readMatrix :: Int -> Int -> IO [[Int]]
readMatrix n m = forM [1 .. n] (\ _ -> (map readInt . words) <$> getLine)

findPos :: [[Int]] -> Int -> Maybe (Int, Int)
findPos matrix number = (,) <$> iIndex <*> jIndex
    where
        jIndices = map (elemIndex number) matrix
        iIndex = findIndex isJust jIndices
        jIndex = msum jIndices

main :: IO ()
main = do
    matrix <- readMatrix 5 5
    let Just (i, j) = findPos matrix 1
    printf "%d" $ ((+) `on` (\x -> abs (x - 2))) i j

import Control.Monad

getI :: [Char] -> Int -> Int
getI [] i = -1
getI (a:as) i | a == '1' = (i + 1) `quot` 2
              | otherwise = getI as (i + 1)

locate :: [[Char]] -> Int -> (Int, Int)
locate [] j = (-1, -1)
locate (a:as) j | getI (a) (-1) == -1 = locate as (j + 1)
                | otherwise = (getI a (-1), j)

dxdy a = abs (2 - fst a) + abs(2 - snd a)
solve a = putStrLn $ show (dxdy (locate (a) (0)))
main = replicateM 5 getLine >>= solve

import Control.Monad
import Data.Array
import Data.Char
import Data.Maybe
import Data.List
import qualified Data.ByteString.Char8 as B
getList :: Read a => IO [a]
getList = fmap (map (read . B.unpack) . B.words) B.getLine
getIntList :: IO [Int]
getIntList = fmap (unfoldr (B.readInt . B.dropWhile isSpace)) B.getLine

solve :: [[Int]] -> Int
solve arr = abs (a - 2) + abs (b - 2)
    where a    = fromJust $ findIndex (elem 1) arr
          arr0 = fromJust $ find (elem 1) arr
          b    = fromJust $ elemIndex 1 arr0

main :: IO ()
main = replicateM 5 getIntList >>= putStrLn . show . solve

import Data.List
import Data.Maybe

getMatrix :: IO Int
getMatrix = do
  mat <- mapM (\_ -> getLine) [1..5]
  let ind = zip mat [1..]
      (m, row) = fromJust $ find (\(m, _) -> '1' `elem` m) ind
      col = 1 + (fromJust $ findIndex (\a -> "1" == a) $ words m)
  return $ abs (col - 3) + abs (row - 3)

main :: IO ()
main = print =<< getMatrix

import Data.List
import Data.Char
import Control.Applicative
import Data.Array

process a b | a<b = "-1"
            |a>b = "1"
            | otherwise = "0"

main::IO ()
main=do
    a<-getContents
    let a1=concatMap (map read) $ map words $ lines  a::[Int]
    let (b,c)=fst$ head  $ filter (\z-> snd z ==1) $ zip ([(i,j)|i<-[1..5],j<-[1..5]]) a1
    print $ (abs (3-b) )+(abs (3-c))

import Control.Arrow (second)
import Data.List (elemIndex)
import Data.Maybe (fromJust, isJust)

main :: IO ()
main = getContents >>= print . solve . map (map read . words) . lines

solve :: [[Int]] -> Int
solve = distance (2, 2) . getIndex 1

getIndex :: Eq a => a -> [[a]] -> (Int, Int)
getIndex n = second fromJust . head . filter (isJust . snd) . zip [0..] . map (elemIndex n)

distance :: Num a => (a, a) -> (a, a) -> a
distance (xa, ya) (xb, yb) = abs (xa - xb) + abs (ya - yb)

import Prelude hiding (readList, showList)
import Control.Monad
import Control.Applicative
import Data.List

readList :: Read a => IO [a]
readList = fmap (fmap read . words) getLine

readList' :: Read a => a -> IO [a]
readList' _ = readList

readInt :: IO Int
readInt = head <$> readList' (undefined::Int)

showList :: Show a => [a] -> IO ()
showList = putStrLn . unwords . map show

skipLine :: IO ()
skipLine = void $ getLine

count :: (a -> Bool) -> [a] -> Int
count f = length . filter f

divUp :: Integral a => a -> a -> a
divUp x y
  | x `mod` y /= 0 = x `div` y + 1
  | otherwise      = x `div` y

main :: IO ()
main = do
  ls <- replicateM 5 $ readList' (undefined :: Int)
  let ls' = transpose ls
      row = sum $ zipWith (*) (fmap sum ls ) [1..]
      col = sum $ zipWith (*) (fmap sum ls') [1..]
  putStrLn $ show $ abs (3-row) + abs (3-col)

import Data.List

col :: String -> Maybe Int
col line = (elemIndex "1" $ words line)

row :: [String] -> Int -> Int
row (line:lines) rowNum = case inRow of
                               Just colNum -> (abs $ rowNum - 2)
                                 + (abs $ colNum - 2)
                               Nothing -> row lines $ rowNum + 1
  where inRow = col line

main = do
  interact $ show . abs . (flip row) 0 . lines

import Data.List
main = print . solve . parse =<< getContents
parse = (`divMod` 5) . head . elemIndices "1" . words
solve (x,y) = abs(x-2) + abs(y-2)

-- import Debug.Trace
-- import System.IO.Unsafe
-- import Text.Printf
import System.IO
import Control.Monad
import Data.List

glwr = fmap (map read . words) getLine

main = do
  hSetBuffering stdout NoBuffering
  solve

solve :: IO ()
solve = do
  m <- forM [1..5] $ const glwr

  print $ beautifulMatrix m

beautifulMatrix :: [[Int]] -> Int
beautifulMatrix m
  = [ abs (x-2) + abs (y-2)
    | y <- [0..4]
    , x <- [0..4]
    , m!!y!!x == 1
    ] !! 0

main=interact$show.f tbl.map read.words
tbl = map (\x ->abs((div x 5)-2)+abs((mod x 5)-2)) [0..24]
f(x:xs)(0:ys)=f xs ys
f(x:_)_=x

main=interact$show.f g.map read.words
g=map(\x->abs(div x 5-2)+abs(mod x 5-2))[0..]
f(x:xs)(0:ys)=f xs ys
f(x:_)_=x

-- Vicfred
-- https://codeforces.com/problemset/problem/263/A

import Control.Monad
import Data.Char
import Data.Maybe
import Data.List

-- see https://stackoverflow.com/questions/29186541/why-is-this-haskell-program-so-much-slower-than-an-equivalent-python-one/29186898
import qualified Data.ByteString.Char8 as B

getIntList :: IO [Int]
getIntList = fmap (unfoldr (B.readInt . B.dropWhile isSpace)) B.getLine
-- getIntList = fmap (map read . words) getLine

solve :: [[Int]] -> Int
solve arr = abs (a - 2) + abs (b - 2)
    where a     = fromJust $ findIndex (elem 1) arr
          arr0  = fromJust $ find (elem 1) arr
          b     = fromJust $ elemIndex 1 arr0

main = replicateM 5 getIntList >>= putStrLn . show . solve

module Main where
import Data.List
import Control.Applicative
import Data.Maybe
findRow :: [String] -> Int
findRow matrix = fromMaybe (-1) $ findIndex (\r -> '1' `elem` r) matrix

findColumn :: String -> Int
findColumn row = fromMaybe (-1) $ elemIndex '1' row

findOne :: [String] -> (Int,Int)
findOne matrix = let rowIndex = findRow matrix in (rowIndex, findColumn $ matrix!!rowIndex)

main = do
  matrix <- mapM (\_ -> filter (/=' ') <$> getLine) [1..5]
  let (row,col) = findOne matrix in print (abs (2 - row) + abs (2 - col))

import Data.Maybe
import Data.List
calcDist (x,y) = (abs (x - 2), (abs (y - 2)))
solve n = let (x,y) = calcDist $ divMod n 5 in x + y
main = interact $ show . solve . fromJust . elemIndex "1" . words

import Data.Char
import Data.List
import Data.Maybe

main = do
	mat <- getContents
	print $ solve $ fromJust $ findIndex (=="1") $ words mat

solve :: Int -> Int
solve pos = abs(x-2) + abs(y-2)
	where
		x = pos `div` 5;
		y = pos `mod` 5;

import Control.Monad

import Data.List (findIndex)

findy :: [[Int]] -> (Int, Int)
findy xs = head [(r, c) | (r, row) <- zip [1..] xs, (c, val) <- zip [1..] row, val == 1]

solve :: [[Int]] -> Int
solve grid =
  let (r, c) = findy grid
  in
  abs (r - 3) + abs (c - 3)

main = do
  grid <- replicateM 5 $ do
    fmap read <$> words <$> getLine
  print $ solve grid

main = interact $ show . calc cost . words
cost = map(\x -> abs(div x 5 - 2) + abs(mod x 5 - 2))[0..]
calc _ [] = 0
calc (c:cs) (x:xs) = calc cs xs + if x == "1" then c else 0

import Control.Applicative

main = do
  matrix <- map words . lines <$> getContents
  let pos = position matrix 0
  print $ abs (fst pos - 2) + abs (snd pos - 2)

position (x:xs) i
  | "1" `elem` x = pos' x 0
  | otherwise    = position xs (i+1)
  where pos' (x:xs) j
          | x == "1"  = (i, j)
          | otherwise = pos' xs (j+1)

import Data.List

inputNumMatrix r=sequence$replicate r(map read.words<$>getLine)

oneCoordinates :: [[Int]] -> (Int, Int)
oneCoordinates matrix@(r:rs) = case findIndex (==1) r of
  Just i -> (0, i)
  Nothing -> (fst nxt + 1, snd nxt)
    where nxt = oneCoordinates rs

distanceFromCenter :: (Int, Int) -> Int
distanceFromCenter (rowI, colI) = abs (rowI - 2) + abs (colI - 2)

main :: IO ()
main = (inputNumMatrix 5 :: IO [[Int]]) >>=
  \matrix -> print $ distanceFromCenter (oneCoordinates matrix)

module Main where

distance :: (Int, Int) -> (Int, Int) -> Int
distance (a,b) (c,d) = abs (a-c) + abs (b-d)

find :: Int -> [Int] -> Int
find k xs = find' 0 k xs
     where
     find' _ _ [] = -1
     find' k x (x':xs) = if x == x'
                         then k
                         else find' (k+1) x xs

extractCoords :: [Int] -> (Int, Int)
extractCoords = extractCoords' 0
              where
              extractCoords' n ((-1):xs) = extractCoords' (n+1) xs
              extractCoords' n (x:xs) = (n, x)

getCoords :: [[Int]] -> (Int, Int)
getCoords mat = extractCoords $ map (find 1) mat

solve :: [[Int]] -> Int
solve mat = distance (2,2) $ getCoords mat

main = do
     matrixStr <- getContents
     let matrixChr = map words $ lines matrixStr
         matrix = map (map (read :: String -> Int)) matrixChr
     print $ solve matrix

import Control.Applicative
import Data.List

main= do
	  s<- map read. words <$> getContents::IO [Int]
	  let x = length $ takeWhile (==0) s
	  let a= mod x 5
	  let b= div x 5
	  print $ abs (a-2) + abs (b-2)

{-
A. Beautiful Matrix
====================
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).
Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).
You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

Input
------
The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

Output
------
Print a single integer — the minimum number of moves needed to make the matrix beautiful.

Sample test(s)
---------------
input
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
output
3

input
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0
output
1

-}

import Data.List (elemIndex, findIndex)
import Data.Maybe (Maybe, isJust)

stepsToBeautifulMatrix :: [[Int]] -> Int
stepsToBeautifulMatrix xxs = abs (i - 2) + abs (j - 2)
  where tmp = map (elemIndex 1) xxs
        Just i = findIndex isJust tmp
        Just j = tmp !! i

main = do
  input <- getContents
  let xxs = [map read (words line) | line <- lines input]
  print $ stepsToBeautifulMatrix xxs

import           Control.Monad                  ( replicateM )
main :: IO ()
main = do
    zeroes <- takeWhile ("1" /=) . concatMap words <$> replicateM 5 getLine
    let pos = length zeroes
    let (row, col) = (pos `div` 5, pos `mod` 5)
    let ans = abs (2 - row) + abs (2 - col)
    print ans

import Control.Monad.State
import Control.Monad (when)

centerCoords = (2,2)

metric::(Int, Int) ->(Int, Int) -> Int
metric (a,b) (c,d) = sum $ abs <$> [a-c, b-d]

getXCoord::[Int]-> Int
getXCoord cc = if all (==0) cc then -1 else length cc  - length (dropWhile (== 0) cc)

readCoords :: IO ()
readCoords = forM_ [0..4] $ \y-> do
    l <- getLine
    let x = getXCoord $  read <$> words l
    Control.Monad.when (x /= - 1) $ print (metric centerCoords (x, y))

someFunc :: IO()
someFunc = readCoords

main :: IO ()
main = someFunc

import Data.List

main :: IO ()
main = interact $ show . solve . elemIndex "1" . words

solve :: Maybe Int -> Int
solve Nothing = 0
solve (Just n) = (abs ((mod n 5) - 2)) + (abs ((div n 5) - 2))

import Data.List
import Data.Maybe

dist :: (Int, Int) -> Int
dist (a, b) =
  (abs (2 - a)) + (abs (2 - b))

extract2 :: [Maybe Int] -> (Int, Int)
extract2 xs =
  (fromJust a, fromJust(xs !! fromJust a)) where
    a = findIndex (isJust) xs

extract :: [String] -> Maybe Int
extract xs =
  findIndex (=="1") xs

lib :: String -> String
lib xs = (show . dist . extract2 . fmap (extract . words) . lines $ xs) ++ "\n"

main :: IO ()
main = interact lib

import Control.Monad
import Control.Applicative
main = replicateM 5 (getLine >>= return.words) >>= return.concat >>= putStrLn.show.fst.head.filter snd.zip distances.map (=="1")

distances = concat $ map (\i -> map (i+) s) s
  where s = map abs [-2..2]

--ghc 7.10

import Data.Maybe
import Data.Tuple

manhattanDistance :: (Num a) => (a,a) -> (a,a) -> a
manhattanDistance (x1,y1) (x2,y2) = (abs (x1-x2)) + (abs (y1-y2))

toMatrix :: Int -> Int -> [a] -> [((Int,Int),a)]
toMatrix n m a = zip indices a
    where indices = [(i,j) | i <- [0..(n-1)], j <- [0..(m-1)]]

beautifulDiff :: (Eq a) => Int -> Int -> a -> [a] -> Int
beautifulDiff n m x a = manhattanDistance mid idx
    where
        mid = (n `div` 2, m `div` 2)
        idx = fromJust $ lookup x (map swap matrix)
        matrix = toMatrix n m a

main = do
    contents <- getContents
    let a = map read . words $ contents :: [Int]
    print $ beautifulDiff 5 5 1 a

import Data.List
import Control.Applicative

main = do
  x <- head . elemIndices 1 . tail . (0:) . map read . words <$> getContents
  print (abs (x `mod` 5 - 2) + abs (x `div` 5 - 2))

import Data.Array
import Data.List
import Control.Applicative
main = do
    ar <- listArray ((1,1),(5,5)) . words <$> getContents :: IO (Array (Int,Int) [Char])
    let c = fst $ head $ filter (\(i,e) -> e=="1") $ assocs ar
    print (m c (3,3))
m (a,b) (c,d) = abs (a-c) + abs (b-d)

distance :: (Int, Int) -> Int
distance (row, column) = abs (row - 2) + abs (column -2)

oneLoc :: String -> (Int, Int)
oneLoc = (`quotRem` 5) . oneDimPos
    where oneDimPos =  length . takeWhile (=="0") . words

solve :: String -> String
solve = show . distance . oneLoc

main :: IO ()
main = interact solve

import Data.Maybe
import Data.List

parseLn = map read . words

findRow = fromJust . findIndex (elem 1)

dist = abs . (-) 2

solve m = dist row + dist col
  where
    row = findRow m
    col = findRow $ transpose m

main = print . solve . map parseLn . lines =<< getContents

import Data.List
f xs = let Just n = elemIndex "1" xs
           [p,q] = [div n 5, mod n 5]
       in abs(p-2)+abs(q-2)
main = interact $ show.f.words

import Control.Monad
import Data.List
import Data.Maybe

removeSpace :: String -> String
removeSpace xs = [ x | x <- xs, not (x `elem` " ") ]

main :: IO ()
main = do
  inputs <- replicateM 5 getLine :: IO [[Char]]
  let ninputs = map removeSpace inputs
  let res = map (\x -> (elemIndex '1' x)) ninputs
  let resm = map (\x -> (fromMaybe (-1) x)) res
  let conc = zip resm [0..(length res)]
  let [(x, y)] = filter (\(a, _) -> a /= -1) conc
  print ((abs (2 - x)) + (abs 2 - y))

main = do
    as <- fmap (map (map read . words) . lines) getContents

    let
        (i, j) = head [(i, j) | i <- [0..4], j <- [0..4], as!!i!!j == 1]

    print $ max (abs (2-i)) (abs (2-j))

main = interact $ show . solve . map read . words

solve xs = abs (2 - y) + abs(2 - y)
    where   (k, _) : _ = filter (\(_,b) -> b == 1) . zip [0..] $ xs
            (x, y) = (mod k 5, div k 5)

import Data.List
import Data.Char
import Control.Applicative
import Data.Array

process a b | a<b = "-1"
            |a>b = "1"
            | otherwise = "0"

main::IO ()
main=do
    getLine
    a<-getContents
    let a1=concatMap (map read) $ map words $ lines  a::[Int]
    let (b,c)=fst$ head  $ filter (\z-> snd z ==1) $ zip ([(i,j)|i<-[1..5],j<-[1..5]]) a1
    print $ (abs (3-b) )+(abs (3-c))


It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.The bus stop queue has n groups of people. The i-th group from the beginning has ai people. Every 30 minutes an empty bus arrives at the bus stop, it can carry at most m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into the current bus, it waits for the next bus together with other groups standing after it in the queue.Your task is to determine how many buses is needed to transport all n groups to the dacha countryside.

The first line contains two integers n and m (1 ≤ n, m ≤ 100). The next line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ m).

Print a single integer — the number of buses that is needed to transport all n groups to the dacha countryside.

{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import Text.Printf

readInt = ( readLn :: IO Int )
getInts = map ( read :: String -> Int ) . words <$> getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	[ n, m ] <- getInts
	as <- getInts

	let
		solve _ [] = 0
		solve s (a:as)
			| s + a <= m = solve ( s + a ) as
			| otherwise = 1 + solve a as

	print $ 1 + solve 0 as

main :: IO()
main = print . solve . map read . tail . words =<< getContents

solve :: [Int] -> Int
solve (m:a) = solve' m a
    where solve' :: Int -> [Int] -> Int
          solve' _ [] = 0
          solve' i (a:x) | a+i > m = 1 + solve' 0 (a:x)
                         | otherwise = solve' (a+i) x

import Control.Monad(liftM)

readi = read :: String -> Int
readL = ((map readi) . words) `liftM` getLine

pc _ x []
    | x /= 0 = 1
    | otherwise = 0
pc m acc (x:xs)
    | acc + x > m = 1 + pc m 0 (x:xs)
    | otherwise = pc m (acc + x) xs

main = do
    [n, m] <- readL
    a <- readL
    putStrLn $ show $ pc m 0 a
    --(sum a + m - 1) `div` m

main :: IO ()
main = getContents >>= print . solve . map read . words

solve :: [Integer] -> Integer
solve (_:m:as) = snd $ foldl f (0, 1) as
  where f (x, y) a | x + a <= m = (x + a, y)
                   | otherwise = (a, y + 1)
solve _ = undefined

main = do
    [n,m] <- fmap (map read . words) getLine
    a <- fmap (map read . words) getLine
    print $ busCount m a

busCount :: Integer -> [Integer] -> Integer
busCount _ [] = 0
busCount m a = 1 + busCount m (drop (length taken) a)
    where variants = map (\n -> sum (take n a)) [1..length a]
          taken = takeWhile (<= m) variants

answ [] _ _ r = r
answ (x:xs) k c r
 | x + c <= k = answ xs k (c + x) r
 | otherwise  = answ xs k x (r + 1)

main :: IO()
main = do
    [_, k] <- (map read . words) `fmap` getLine
    xs <- (map read . words) `fmap` getLine
    print $ answ xs k 0 1

solve :: Int -> [Int] -> Int
solve m [_] = 1
solve m (g1:g2:gs)
  | g1 + g2 <= m = solve m (g1+g2:gs)
  | otherwise    = 1 + solve m (g2:gs)

main :: IO ()
main = do
  [_, m] <- fmap (map read . words) getLine :: IO [Int]
  gs <- fmap (map read . words) getLine :: IO [Int]
  print $ solve m gs

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
-- hiding clauses are to allow Data.Foldable's generalizations
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, foldl1, foldr, foldr1, and, or, any, all, sum, product, concat, concatMap, maximum, minimum, elem, notElem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (mapM, mapM_, forM, forM_, sequence, sequence_, msum)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List hiding (foldl, foldl', foldl1, foldl1', foldr, foldr1, concat, concatMap, and, or, any, all, sum, product, maximum, minimum, elem, notElem, find)
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Traversable
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
-- stolen from lens:
a & f = f a
a <&> f = fmap f a
infixl 1 &, <&>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)
-- }}}
-- input and output {{{
bsGetLine :: IO ByteString
bsGetLine = fst . BS.spanEnd isSpace <$> BS.getLine

inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

buses :: Int -> [Int] -> Int
buses m = go 0
	where
		go 0 [] = 0
		go _ [] = 1
		go r (y:ys)
			| r + y <= m = go (r + y) ys
			| otherwise  = 1 + go y ys

main :: IO ()
main = do
	[_, m] <- inputInts
	xs <- inputInts
	print $ buses m xs

import Control.Applicative
import Data.List

process n _ c [] =n + if c>0 then 1 else 0
process n m c (s:ss) | c+s<m = process n m (c+s) ss
                     | c+s==m = process (n+1) m 0 ss
		     | c+s>m = process (n+1) m s ss

main= do
	[n,m]<- map read.words  <$> getLine:: IO [Int]
	s<-map read.words  <$> getLine:: IO [Int]
	print $ process 0 m 0 s

main = do
    let i = fmap (map read . words) getLine :: IO [Int]
    [_, m] <- i
    a <- i
    print $ gao a m m

gao :: [Int] -> Int -> Int -> Int
gao [] _ _ = 1
gao a@(x:xs) rem n
    | x <= rem  = gao xs (rem - x) n
    | otherwise = gao a n n + 1

main :: IO ()
main = do
  _:m:_ <- fmap (map read . words) getLine
  ls <- fmap (map read . words) getLine
  print $ rush m ls 0

rush :: Int -> [Int] -> Int -> Int
rush _ [] n = n
rush m ls n = rush m (go ls 0) (n + 1)
  where go [] _ = []
        go l@(x:r) a
          | a + x > m = l
          | otherwise = go r (a + x)

main :: IO ()
main = do
	input <- getContents
	ls <- return $ lines input
	nm <- return $ words $ ls!!0
	l2 <- return $ words $ ls!!1
	putStr $ solve (read $ nm!!0) (read $ nm!!1) (map read l2)

solve :: Int -> Int -> [Int] -> String
solve n m as = show $ f m as where
	f :: Int -> [Int] -> Int
	f r [] = if r < m then 1 else 0
	f 0 ls = 1 + (f m ls)
	f r all@(l:ls) = if l <= r then f (r-l) ls else 1+(f m all)

parseInput input = (n, m, xs) where
    ls = lines input
    [n, m] = map read $ words $ head ls :: [Int]
    xs = map read $ words $ last ls :: [Int]

solve _ [] buses _ = buses
solve m xs buses seats  | head xs + seats <= m  = solve m (tail xs) buses (seats + head xs)
                        | otherwise             = solve m (tail xs) (buses + 1) (head xs)

main = do
    input <- getContents
    let (n, m, xs) = parseInput input
    print $ solve m xs 1 0

import Control.Monad(liftM)

readi = read :: String -> Int
readL = ((map readi) . words) `liftM` getLine

main = do
    [n, m] <- readL
    a <- readL
    putStrLn $ show $ (sum a + m - 1) `div` m

main :: IO()
main = do
    [_, k] <- (map read . words) `fmap` getLine
    xs <- (map read . words) `fmap` getLine
    print $ ((sum xs) `quot` k) + if (((sum xs) `rem` k) > 0) then 1 else 0

main :: IO ()
main = do
  [n, m] <- fmap (map read . words) getLine :: IO [Int]
  as <- fmap (map read . words) getLine :: IO [Int]
  print $ toInteger $ ceiling (fromIntegral (sum as) / fromIntegral m)

import Control.Applicative
import Data.List

process n _ _ [] =n
process n m c (s:ss) | c+s<m = process n m (c+s) ss
                     | c+s==m = process (n+1) m 0 ss
		     | c+s>m = process (n+1) m s ss

main= do
	[n,m]<- map read.words  <$> getLine:: IO [Int]
	s<-map read.words  <$> getLine:: IO [Int]
	print $ process 0 m 0 s

main = do
    let i = fmap (map read . words) getLine :: IO [Int]
    [_, m] <- i
    a <- i
    print $ gao a m m

gao :: [Int] -> Int -> Int -> Int
gao [_] _ _ = 1
gao a@(x:xs) rem n
    | x <= rem  = gao xs (rem - x) n
    | otherwise = gao a n n + 1

main = do
    let i = fmap (map read . words) getLine :: IO [Int]
    [n, m] <- i
    a <- i
    print $ gao a n n

gao :: [Int] -> Int -> Int -> Int
gao [_] _ _ = 1
gao a@(x:xs) rem n
    | x <= rem  = gao xs (rem - x) n
    | otherwise = gao a n n + 1