From fa811780b391c3687ce1cf477515cb82b5bd345d Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 2 Dec 2021 10:45:26 +0200 Subject: Day 02 --- day02/main.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 day02/main.hs (limited to 'day02/main.hs') 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 -- cgit v1.3