From 6ca8baf1b58b9faf8f07364e2212dd9b84bd149a Mon Sep 17 00:00:00 2001 From: germax26 Date: Mon, 2 Dec 2024 21:14:08 +1100 Subject: [PATCH] 2015 Day 02 in Haskell --- 2015/day02/day02.hs | 16 ++++++++++++++++ aoc.hs | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 2015/day02/day02.hs diff --git a/2015/day02/day02.hs b/2015/day02/day02.hs new file mode 100644 index 0000000..30e5c92 --- /dev/null +++ b/2015/day02/day02.hs @@ -0,0 +1,16 @@ +import Aoc +import Data.List.Split + +parseFile :: [String] -> [(Int, Int, Int)] +parseFile = map $ triplet . map read . splitOn "x" + +part1 :: [(Int, Int, Int)] -> Int +part1 = sum . map wrapping + where wrapping (l, w, h) = let areas = [l*w, l*h, w*h] in 2*sum areas + minimum areas + +part2 :: [(Int, Int, Int)] -> Int +part2 = sum . map wrapping + where wrapping (l, w, h) = 2*minimum [l+w, l+h, w+h] + l*w*h + +main :: IO () +main = aocMain parseFile part1 part2 \ No newline at end of file diff --git a/aoc.hs b/aoc.hs index da16c4a..d1098aa 100644 --- a/aoc.hs +++ b/aoc.hs @@ -9,4 +9,13 @@ aocMain parseFile part1 part2 = do print $ "Part 2: " ++ show (part2 input) enumerate :: [a] -> [(Int, a)] -enumerate xs = zip [0..length xs] xs \ No newline at end of file +enumerate xs = zip [0..length xs] xs + +unimplemented :: Show p => p -> Int +unimplemented = error . show + +pair :: [a] -> (a, a) +pair as = let [a1, a2] = as in (a1, a2) + +triplet :: [a] -> (a, a, a) +triplet as = let [a1, a2, a3] = as in (a1, a2, a3) \ No newline at end of file