Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,15 @@
import Data.Array (Array, array, bounds, range, (!))
import Text.Printf (printf)
import Data.List (sortBy)
compZig :: (Int, Int) -> (Int, Int) -> Ordering
compZig (x, y) (x_, y_) = compare (x + y) (x_ + y_) <> go x y
where
go x y
| even (x + y) = compare x x_
| otherwise = compare y y_
zigZag :: (Int, Int) -> Array (Int, Int) Int
zigZag upper = array b $ zip (sortBy compZig (range b)) [0 ..]
where
b = ((0, 0), upper)

View file

@ -0,0 +1,12 @@
-- format a 2d array of integers neatly
show2d a =
unlines
[ unwords
[ printf "%3d" (a ! (x, y) :: Integer)
| x <- axis fst ]
| y <- axis snd ]
where
(l, h) = bounds a
axis f = [f l .. f h]
main = mapM_ (putStr . ('\n' :) . show2d . zigZag) [(3, 3), (4, 4), (10, 2)]

View file

@ -0,0 +1,26 @@
import Data.Text (justifyRight, pack, unpack)
import Data.List (mapAccumL)
import Data.Bool (bool)
zigZag :: Int -> [[Int]]
zigZag = go <*> diagonals
where
go _ [] = []
go n xss = (head <$> edge) : go n (dropWhile null (tail <$> edge) <> rst)
where
(edge, rst) = splitAt n xss
diagonals :: Int -> [[Int]]
diagonals n =
snd $ mapAccumL go [0 .. (n * n) - 1] (slope <> [n] <> reverse slope)
where
slope = [1 .. n - 1]
go xs h = (rst, bool id reverse (0 /= mod h 2) grp)
where
(grp, rst) = splitAt h xs
main :: IO ()
main =
putStrLn $
unlines $
concatMap unpack . fmap (justifyRight 3 ' ' . pack . show) <$> zigZag 5