aoc-2021/day2/first.hs

24 lines
708 B
Haskell

data Direction = Forward | Up | Down
deriving (Show, Eq)
loop :: Integer -> Integer -> [(Direction, Integer)] -> Integer
loop horizontal depth [] = horizontal * depth
loop horizontal depth ((dir, mag):rest) = case dir of
Forward -> loop (horizontal + mag) depth rest
Up -> loop horizontal (depth - mag) rest
Down -> loop horizontal (depth + mag) rest
-- helper functions
stringToDirection :: String -> Direction
stringToDirection str = case str of
"forward" -> Forward
"up" -> Up
"down" -> Down
convert :: [String] -> [(Direction, Integer)]
convert [] = []
convert (a:b:rest) = (stringToDirection a, read b):convert rest
main = interact $ show . loop 0 0 . convert . words