adventofcode-2021/2/navigator.hs

58 lines
1.6 KiB
Haskell
Raw Normal View History

import Control.Exception (assert)
2021-12-03 03:35:15 +00:00
main :: IO ()
main = do
instructions <- getContents
putStrLn (solution1 instructions)
2021-12-03 21:59:05 +00:00
putStrLn (solution2 instructions)
solution1 :: String -> String
solution1 input = show (uncurry (*) (navigate (parseInstructions input)))
2021-12-03 03:35:15 +00:00
2021-12-03 21:59:05 +00:00
solution2 :: String -> String
solution2 input = show (uncurry (*) (navigate2 (parseInstructions input)))
2021-12-03 03:35:15 +00:00
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)
2021-12-03 21:59:05 +00:00
navigate2 :: [(Int, Int)] -> (Int, Int)
navigate2 instructions =
takePosition (foldl navigate (0, 0, 0) instructions)
where
navigate :: (Int, Int, Int) -> (Int, Int) -> (Int, Int, Int)
navigate (horz, depth, aim) (speed, dir) = (horz + speed, depth + aim * speed, aim + dir)
takePosition :: (Int, Int, Int) -> (Int, Int)
takePosition (x,y,_) = (x,y)
-- Tests
testInput1 :: String
testInput1 = unlines [
"forward 5",
"down 5",
"forward 8",
"up 3",
"down 8",
"forward 2"
]
test1 :: String
test1 = assert ((solution1 testInput1) == "150") "success"
2021-12-03 21:59:05 +00:00
test2 :: String
test2 = assert ((solution2 testInput1) == "900") "success"