From 5954e0f299f0b412e377d9f9eaac34a2230a0a7e Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 1 Nov 2024 00:49:17 +0000 Subject: Puzzle 2 2015 done --- 2015/puzzle-2.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2015/puzzle-2.hs (limited to '2015/puzzle-2.hs') diff --git a/2015/puzzle-2.hs b/2015/puzzle-2.hs new file mode 100644 index 0000000..3fbbd65 --- /dev/null +++ b/2015/puzzle-2.hs @@ -0,0 +1,28 @@ +import qualified Data.Text as DT +import qualified Data.List as DL + +parse_line s = map read_int members + where + delim = DT.pack "x" + content = DT.pack s + members = map DT.unpack $ DT.splitOn delim content + read_int = \x -> (read x) :: Int + +surface_area [l, w, h] = (2 * l * w) + (2 * w * h) + (2 * l * h) +extra_area xs = x * y + where [x, y] = take 2 $ DL.sort xs +total_area xs = (surface_area xs) + (extra_area xs) + +volume [l, w, h] = l * w * h +ribbon_wrap xs = (2 * x) + (2 * y) + where [x, y] = take 2 $ DL.sort xs + +total_ribbon xs = (volume xs) + (ribbon_wrap xs) + +main = do + content <- readFile "2-input" + let dimensions = map parse_line $ lines content + let areas = sum $ map total_area dimensions + let ribbon = sum $ map total_ribbon dimensions + putStrLn $ "Round 1: " ++ (show areas) + putStrLn $ "Round 2: " ++ (show ribbon) -- cgit v1.2.3-13-gbd6f