haskell: 2015 02b
This commit is contained in:
parent
fee1d10eb7
commit
227b01bd0f
|
@ -36,7 +36,8 @@ executable haskell
|
||||||
-- LANGUAGE extensions used by modules in this package.
|
-- LANGUAGE extensions used by modules in this package.
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.16.4.0,
|
base >= 4.16.4.0,
|
||||||
|
text >= 2.1.1,
|
||||||
aoc2015,
|
aoc2015,
|
||||||
|
|
||||||
hs-source-dirs: app
|
hs-source-dirs: app
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
module Day02 (parse, solveA, solveB) where
|
module Day02 (parse, solveA, solveB) where
|
||||||
|
|
||||||
|
import Data.List (sort, tails)
|
||||||
|
|
||||||
splitOn :: (Eq a) => a -> [a] -> [[a]]
|
splitOn :: (Eq a) => a -> [a] -> [[a]]
|
||||||
splitOn _ [] = []
|
splitOn _ [] = []
|
||||||
splitOn delimiter xs =
|
splitOn delimiter xs =
|
||||||
|
@ -8,23 +10,26 @@ splitOn delimiter xs =
|
||||||
[] -> []
|
[] -> []
|
||||||
_ : rest -> splitOn delimiter rest
|
_ : rest -> splitOn delimiter rest
|
||||||
|
|
||||||
parse :: String -> [(Int, Int, Int)]
|
pairs :: [a] -> [(a, a)]
|
||||||
parse = map toTuple . lines
|
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
|
where
|
||||||
toTuple :: String -> (Int, Int, Int)
|
calculate :: [Int] -> Int
|
||||||
toTuple str = let [a, b, c] = splitOn 'x' str in (read a, read b, read c)
|
calculate sides = (sum . map (2 *)) sides + minimum sides
|
||||||
|
|
||||||
toArea :: (Int, Int, Int) -> Int
|
solveA :: [[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 = sum . map toArea
|
solveA = sum . map toArea
|
||||||
|
|
||||||
-- >>> map toArea $ parse "2x3x4"
|
solveB :: [[Int]] -> Int
|
||||||
-- [64]
|
solveB = sum . map calcWrap
|
||||||
|
where
|
||||||
solveB = undefined
|
calcWrap :: [Int] -> Int
|
||||||
|
calcWrap dims =
|
||||||
|
let wrap = (2 *) . sum . take 2 $ sort dims
|
||||||
|
bow = product dims
|
||||||
|
in wrap + bow
|
||||||
|
|
Loading…
Reference in a new issue