adventofcode-2021/2/navigator.hs

26 lines
772 B
Haskell

main :: IO ()
main = do
instructions <- getContents
let
parsed = parseInstructions instructions
location = navigate parsed
solution1 = uncurry (*) location
print solution1
parseInstructions :: String -> [(Int, Int)]
parseInstructions instructions =
map parseLine (lines instructions)
where
parseLine line = case break (== ' ') line of
("forward", distance) -> (read distance, 0)
("down", distance) -> (0, read distance)
("up", distance) -> (0, (-read distance))
_ -> error "Invalid line"
navigate :: [(Int, Int)] -> (Int, Int)
navigate instructions =
foldl1 sumTuples instructions
where
sumTuples :: (Int, Int) -> (Int, Int) -> (Int, Int)
sumTuples (a, b) (c, d) = (a + c, b + d)