adventofcode-2021/14/Parsing.hs

33 lines
917 B
Haskell

module Parsing (
splitByString,
parseCoordinates
) where
import Data.List (isPrefixOf)
splitByString :: String -> String -> [String]
splitByString _ "" = []
splitByString splitter string =
let (chunk, rest) = spanNextSplit string
in
chunk:(splitByString splitter rest)
where
spanNextSplit :: String -> (String, String)
spanNextSplit [] = ([], [])
spanNextSplit everything@(char:rest)
| splitter `isPrefixOf` rest =
([char], (drop ((length splitter) + 1) everything))
| otherwise =
let
(start, end) = spanNextSplit rest
in
(char:start, end)
parseCoordinates :: String -> [(Int, Int)]
parseCoordinates =
map (tuplify . map read . splitByString ",") . lines
where
tuplify :: [a] -> (a, a)
tuplify [a, b] = (a, b)
tuplify _ = error "Can't parse coordinates from non-2-sized lists"