never executed always true always false
    1 {-|
    2 Module:      Y2015.D24
    3 Description: Advent of Code Day 24 Solutions.
    4 License:     MIT
    5 Maintainer:  @tylerjl
    6 
    7 Solutions to the day 24 set of problems for <adventofcode.com>.
    8 -}
    9 
   10 module Y2015.D24
   11     ( idealEntanglement
   12     , idealEntanglementOptimized
   13     )
   14 where
   15 
   16 import Data.Function (on)
   17 import Data.List     (find, groupBy, minimumBy, sortBy, subsequences)
   18 import Data.Ord      (comparing)
   19 
   20 -- |Find the ideal entanglement value for a given input of packages.
   21 idealEntanglement :: Int    -- ^ How many compartments to divide amongst.
   22                   -> String {-^
   23                                 Input as a string containing newlined-separated
   24                                 list of package weights.
   25                             -}
   26                   -> Int    {-^
   27                                 Ideal entanglement value for the resulting
   28                                 package distribution.
   29                             -}
   30 idealEntanglement compartments input =
   31     product $ minimumBy (mconcat santasPredicates) seqs
   32     where weights  = map read $ lines input
   33           balanced = sum weights `quot` compartments
   34           isBalanced = (==) balanced . sum
   35           seqs = filter isBalanced $ subsequences weights
   36           santasPredicates = [comparing length, comparing product]
   37 
   38 -- |Find the ideal entanglement value for a given input of packages.
   39 idealEntanglementOptimized :: Int    -- ^ How many compartments to divide amongst.
   40                   -> String    {-^
   41                                    Input as a string containing newlined-separated
   42                                    list of package weights.
   43                                -}
   44                   -> Maybe Int {-^
   45                                    Ideal entanglement value for the resulting
   46                                    package distribution if one can be found.
   47                                -}
   48 idealEntanglementOptimized compartments input =
   49     case seqs of
   50         Just groups -> Just $ minimum $ map product groups
   51         Nothing     -> Nothing
   52     where weights  = map read $ lines input
   53           balanced = sum weights `quot` compartments
   54           isBalanced = (==) balanced . sum
   55           seqs = find (any isBalanced) $
   56               groupBy ((==) `on` length) $
   57               sortBy (compare `on` length) $
   58               subsequences weights