aboutsummaryrefslogtreecommitdiffstats
path: root/day02/main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'day02/main.hs')
-rw-r--r--day02/main.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/day02/main.hs b/day02/main.hs
new file mode 100644
index 0000000..ac740ce
--- /dev/null
+++ b/day02/main.hs
@@ -0,0 +1,35 @@
+module Main where
+
+import Data.Foldable (Foldable (foldl'))
+import Utils
+
+toDelta :: (String, Int) -> (Int, Int)
+toDelta ("up", n) = (0, - n)
+toDelta ("down", n) = (0, n)
+toDelta ("forward", n) = (n, 0)
+toDelta x = error $ "invalid instruction: " ++ show x
+
+e1 :: [(String, Int)] -> Int
+e1 =
+ map toDelta
+ .> foldl' (\(ax, ay) (x, y) -> (ax + x, ay + y)) (0, 0)
+ .> uncurry (*)
+
+e2 :: [(String, Int)] -> Int
+e2 =
+ map toDelta
+ .> foldl' (\(ax, ay, aaim) (x, aim) -> (ax + x, ay + aaim * x, aaim + aim)) (0, 0, 0)
+ .> \(x, y, _) -> x * y
+
+main :: IO ()
+main =
+ do
+ contents <- getContents
+ let input =
+ contents
+ $> lines
+ .> map words
+ .> map (\[x, y] -> (x, read y))
+
+ e1 input $> print
+ e2 input $> print