-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lib.hs
30 lines (24 loc) · 873 Bytes
/
Lib.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Lib (readFile', tRead, buildGrid, split, count) where
import Data.ByteString qualified as B (readFile)
import Data.Map qualified as M
import Data.Text (Text)
import Data.Text.Encoding qualified as T (decodeUtf8)
import Data.Text.Read qualified as TR
-- IO --
readFile' :: FilePath -> IO Text
readFile' f = T.decodeUtf8 <$> B.readFile f
-- Data.Text --
tRead :: Text -> Int
tRead = (\(Right (n, _)) -> n) . TR.signed TR.decimal
-- Data.Map --
buildGrid :: [[a]] -> M.Map (Int, Int) a
buildGrid xs = M.fromList [((x, y), a) | (row, x) <- zip xs [0 ..], (a, y) <- zip row [0 ..]]
-- Utility --
split :: (Eq a) => a -> [a] -> [[a]]
split delimiter list =
let (before, after) = break (== delimiter) list
in before : case after of
[] -> []
(_ : xs) -> split delimiter xs
count :: (Eq a) => [a] -> a -> Int
count xs x = length $ filter (x ==) xs