haskell: 2015 02b

This commit is contained in:
Maciej Jur 2024-08-01 00:22:03 +02:00
parent fee1d10eb7
commit 227b01bd0f
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
2 changed files with 23 additions and 17 deletions

View file

@ -36,7 +36,8 @@ executable haskell
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends:
base >=4.16.4.0,
base >= 4.16.4.0,
text >= 2.1.1,
aoc2015,
hs-source-dirs: app

View file

@ -1,5 +1,7 @@
module Day02 (parse, solveA, solveB) where
import Data.List (sort, tails)
splitOn :: (Eq a) => a -> [a] -> [[a]]
splitOn _ [] = []
splitOn delimiter xs =
@ -8,23 +10,26 @@ splitOn delimiter xs =
[] -> []
_ : rest -> splitOn delimiter rest
parse :: String -> [(Int, Int, Int)]
parse = map toTuple . lines
pairs :: [a] -> [(a, a)]
pairs xs = [(x, y) | (x : ys) <- tails xs, y <- ys]
parse :: String -> [[Int]]
parse = map (map read . splitOn 'x') . lines
toArea :: [Int] -> Int
toArea = calculate . map (uncurry (*)) . pairs
where
toTuple :: String -> (Int, Int, Int)
toTuple str = let [a, b, c] = splitOn 'x' str in (read a, read b, read c)
calculate :: [Int] -> Int
calculate sides = (sum . map (2 *)) sides + minimum sides
toArea :: (Int, Int, Int) -> Int
toArea (l, w, h) =
let a = l * w
b = w * h
c = h * l
in 2 * a + 2 * b + 2 * c + min a (min b c)
solveA :: [(Int, Int, Int)] -> Int
solveA :: [[Int]] -> Int
solveA = sum . map toArea
-- >>> map toArea $ parse "2x3x4"
-- [64]
solveB = undefined
solveB :: [[Int]] -> Int
solveB = sum . map calcWrap
where
calcWrap :: [Int] -> Int
calcWrap dims =
let wrap = (2 *) . sum . take 2 $ sort dims
bow = product dims
in wrap + bow