summaryrefslogtreecommitdiff
path: root/4-kyu/snail.hs
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-04-24 17:54:22 +0800
committerMistivia <i@mistivia.com>2025-04-24 17:54:22 +0800
commitda0b3584bc6dea9f02b9ddb949dbf8f404228a1a (patch)
tree6d478d47eb002c3cd8b09244a1ce00dc69ba9e79 /4-kyu/snail.hs
parent7ac9846329cc231c79fa1158f1e1f1d5fc96bc22 (diff)
update
Diffstat (limited to '4-kyu/snail.hs')
-rw-r--r--4-kyu/snail.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/4-kyu/snail.hs b/4-kyu/snail.hs
new file mode 100644
index 0000000..20512e0
--- /dev/null
+++ b/4-kyu/snail.hs
@@ -0,0 +1,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)