From 6ca4b02c6761fc56984489896a3e7cb0a74a3941 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Mon, 27 Nov 2023 16:14:16 -0500 Subject: [PATCH] Start of magic square --- src/hackerRank/algorithms-magicSquare.hs | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/hackerRank/algorithms-magicSquare.hs diff --git a/src/hackerRank/algorithms-magicSquare.hs b/src/hackerRank/algorithms-magicSquare.hs new file mode 100644 index 0000000..5d0fa37 --- /dev/null +++ b/src/hackerRank/algorithms-magicSquare.hs @@ -0,0 +1,37 @@ +-- https://www.hackerrank.com/challenges/magic-square-forming/problem + +module Main where +import Data.List (transpose) + +main :: IO () +main = print "test" + +s :: [[Integer]] +s = + [ [5, 3, 4] + , [1, 5, 8] + , [6, 4, 2] + ] + +-- isMagicSquare n s = +-- all (sum rows == n) && +-- all (sum cols == n) && +-- all (sum diag == n) +-- where +-- rows = + +get s (x,y) = s !! x !! y + +allCoords n = [ (x,y) | x <- [0..n-1], y <- [0..n-1] ] + +diagCoords n = [ (x,y) | x <- [0..n-1], y <- [0 ..n-1], x == y ] ++ + [ (x,y) | x <- [0..n-1], y <- [n-1, n-2.. 0], x + y == n-1 ] + +rowSums :: [[Integer]] -> [Integer] +rowSums = map sum + +colSums :: [[Integer]] -> [Integer] +colSums = map sum . transpose + +diagSums :: [[Integer]] -> [Integer] +diagSums n = map (\(x,y) -> get n (x,y)) (diagCoords 3)