30 lines
834 B
Haskell
30 lines
834 B
Haskell
module Day02 where
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
import Text.Read (readMaybe)
|
|
|
|
data Move = Horizontal Int | Depth Int deriving Show
|
|
|
|
readDataLines:: (String -> Maybe a) -> String -> Either String [a]
|
|
readDataLines f s = case mapM f (lines s) of
|
|
Just xs -> Right xs
|
|
Nothing -> Left "Error parsing string"
|
|
|
|
day02Part1 :: String -> String
|
|
day02Part1 _ = "Day02 Part1"
|
|
|
|
|
|
day02Part2 :: String -> String
|
|
day02Part2 _ = "Day02 Part2"
|
|
|
|
readDirection ::String -> Maybe (Int -> Move)
|
|
readDirection "forward" = Just Horizontal
|
|
readDirection "down" = Just Depth
|
|
readDirection "up" = Just (Depth . negate)
|
|
readDirection _ = Nothing
|
|
|
|
readMove :: String -> Maybe Move
|
|
readMove s = case words s of
|
|
[dirStr, distStr] -> readDirection dirStr <*> readMaybe distStr
|
|
_ -> Nothing
|