diff --git a/haskellAoC/inputs/2015/15 b/haskellAoC/inputs/2015/15 new file mode 100644 index 0000000..6281182 --- /dev/null +++ b/haskellAoC/inputs/2015/15 @@ -0,0 +1,4 @@ +Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5 +PeanutButter: capacity -1, durability 3, flavor 0, texture 0, calories 1 +Frosting: capacity 0, durability -1, flavor 4, texture 0, calories 6 +Sugar: capacity -1, durability 0, flavor 0, texture 2, calories 8 diff --git a/haskellAoC/src/Y2015/Day15.hs b/haskellAoC/src/Y2015/Day15.hs new file mode 100644 index 0000000..9edc2e2 --- /dev/null +++ b/haskellAoC/src/Y2015/Day15.hs @@ -0,0 +1,48 @@ +module Y2015.Day15 (y15day15) where + +import Data.List + +data Ingredient = Ingredient + { name :: String + , properties :: [Int] + , calories :: Int + } deriving (Show) + +parseLine :: String -> Ingredient +parseLine input = Ingredient n props cal + where input' = words $ filter (/= ',') input + n = init $ input' !! 0 + props = map (read . (input' !!)) [2, 4, 6, 8] + cal = read $ input' !! 10 + +negIsZero :: Int -> Int +negIsZero n + | n < 0 = 0 + | otherwise = n + +computeScore :: [(Int, Ingredient)] -> Int +computeScore qtyIngr = product $ map (negIsZero) $ map (sum) $ transpose $ map ingredientProps qtyIngr + where ingredientProps (qty, ingr) = map (* qty) $ properties ingr + +computeScore2 :: [(Int, Ingredient)] -> Int +computeScore2 qtyIngr = product $ propertiesSums' + where ingredientProps (qty, ingr) = map (* qty) $ (properties ingr ++ [calories ingr]) + has500cal = (last propertiesSums) == 500 + propertiesSums = map (negIsZero) $ map (sum) $ transpose $ map ingredientProps qtyIngr + propertiesSums' = case has500cal of + True -> init propertiesSums + False -> [0] + +getCombQty :: Int -> [Ingredient] -> [[(Int, Ingredient)]] +getCombQty 0 _ = [] +getCombQty _ [] = [] +getCombQty nb (i:[]) = [[(nb, i)]] +getCombQty nb (i:is) = concatMap (\qty -> map (\is' -> ((qty,i):is')) $ getCombQty (nb - qty) is) [1..nb] + +y15day15 :: [String] -> (String, String) +y15day15 input = (part1, part2) + where part1 = show $ maximum $ map computeScore $ combQties + part2 = show $ maximum $ map computeScore2 $ combQties + + ingredients = map parseLine input + combQties = getCombQty 100 ingredients diff --git a/haskellAoC/src/Y2015/Days.hs b/haskellAoC/src/Y2015/Days.hs index c645dab..53df661 100644 --- a/haskellAoC/src/Y2015/Days.hs +++ b/haskellAoC/src/Y2015/Days.hs @@ -14,6 +14,7 @@ import Y2015.Day11 import Y2015.Day12 import Y2015.Day13 import Y2015.Day14 +import Y2015.Day15 year2015 :: String -> [String] -> (String, String) year2015 "01" = y15day01 @@ -30,3 +31,4 @@ year2015 "11" = y15day11 year2015 "12" = y15day12 year2015 "13" = y15day13 year2015 "14" = y15day14 +year2015 "15" = y15day15