summaryrefslogtreecommitdiff
path: root/4-kyu/snail.hs
blob: 20512e0e946c8dbb063abd696778e1aa25389edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1

module Snail where

snail :: [[Int]] -> [Int]
snail [] = []
snail arr = foldl1 (++) $ map (roundAt w h arr) [0..center]
    where
        w = length (arr !! 0)
        h = length arr
        center = min (w `div` 2) (h `div` 2)

rowRange col from to arr =
    drop from $ take (to+1) $ arr !! col

colRange row from to arr =
    map (!!row) $ drop from $ take (to+1) arr

roundAt w h arr x =
    (rowRange x x (w-x-1) arr) ++
        (colRange (w-x-1) (x+1) (h-x-1) arr) ++
            (reverse $ rowRange (h-x-1) x (w-x-2) arr) ++
                (reverse $ colRange x (x+1) (h-x-2) arr)