Files
advent-of-code-2023/day23/Commons.hs
2023-12-23 14:58:01 +01:00

33 lines
1.1 KiB
Haskell
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module Commons where
import GHC.IO.Handle (isEOF)
import Data.Array (Array, listArray, (!), Ix (inRange), bounds)
data Direction = North | South | West | East deriving (Eq, Ord, Show)
data Tile = Empty | Rock | Slope { direction :: Direction } deriving (Eq, Show)
type Trails = Array (Int, Int) Tile
parseLine :: String -> [Tile]
parseLine = map (\case
'.' -> Empty
'#' -> Rock
'>' -> Slope {direction = East}
'v' -> Slope {direction = South}
'<' -> Slope {direction = West}
'^' -> Slope {direction = North})
parseTrails :: IO [[Tile]]
parseTrails = do done <- isEOF
if done
then return []
else do line <- getLine
let trailsLine = parseLine line
trails <- parseTrails
return (trailsLine: trails)
parse :: IO Trails
parse = do trails <- parseTrails
return $ listArray ((1, 1), (length trails, length $ head trails)) $ concat trails