Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2024-12-04 13:01:05 +05:30
parent 721678db97
commit 1dc5135987
3 changed files with 89 additions and 1 deletions

View File

@@ -1,10 +1,29 @@
module AoC where
import Text.Parsec
import Data.List (transpose)
import Text.Parsec (ParseError)
-- extract Right value after parsing
extract :: Either ParseError a -> a
extract (Left err) = error ("Parsing failed: " ++ show err)
extract (Right val) = val
-- count elements in a list
count :: (Eq a) => a -> [a] -> Int
count x = length . filter (x ==)
-- extract diagonals from a matrix
diagonals :: [[a]] -> [[a]]
diagonals =
(++)
<$> reverse . transpose . zipWith drop [0 ..]
<*> transpose . zipWith drop [1 ..] . transpose
-- get indices of substring sub in str
findSubstrings :: [Char] -> [Char] -> [Int]
findSubstrings sub str = findSubstrings' sub str 0
where
findSubstrings' _ [] _ = []
findSubstrings' sub str@(x : xs) idx
| take (length sub) str == sub = idx : findSubstrings' sub xs (idx + 1)
| otherwise = findSubstrings' sub xs (idx + 1)