Something went wrong. Try again.
advent of code solutions op.tngl.io
Something went wrong. Try again.
12345678910111213141516171819202122232425262728{-# LANGUAGE LambdaCase #-}
import System.Environment (getArgs)
ans1 = foldl mv (0, 0) where mv (f, d) ["forward", i] = (f + read i, d) mv (f, d) ["up", i] = (f, d - read i) mv (f, d) ["down", i] = (f, d + read i) mv x _ = x
ans2 = foldl mv (0, 0, 0) where mv (f, d, a) ["forward", i] = (f + read i, d + a * read i, a) mv (f, d, a) ["up", i] = (f, d, a - read i) mv (f, d, a) ["down", i] = (f, d, a + read i) mv x _ = x
main = do n <- fmap (map words . lines) $ getArgs >>= \case ["-"] -> getContents [file] -> readFile file print $ uncurry (*) $ ans1 n print $ (\(f, d, _) -> f * d) $ ans2 n