2015 Day 02 in Haskell

This commit is contained in:
germax26 2024-12-02 21:14:08 +11:00
parent 6e4b95af18
commit 6ca8baf1b5
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
2 changed files with 26 additions and 1 deletions

16
2015/day02/day02.hs Normal file
View File

@ -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

9
aoc.hs
View File

@ -10,3 +10,12 @@ aocMain parseFile part1 part2 = do
enumerate :: [a] -> [(Int, a)] enumerate :: [a] -> [(Int, a)]
enumerate xs = zip [0..length xs] xs 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)