summaryrefslogtreecommitdiff
path: root/2015/day17.hs
blob: 9b5af87f1d72669d10806c36720b2e22c84f4da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import Control.Monad

search :: Int -> [Int] -> [[Int]]
search 0 [] = [[]]
search _ [] = []
search target (sz:szs)
    | target < 0  = []
    | target == 0 = [[]]
    | target < sz = search target szs
    | otherwise   = map (sz:) (search (target-sz) szs) ++ search target szs

day17 :: IO ()
day17 = do
    input <- liftM (map read . lines) $ readFile "day17.txt"
    let fullsearch = search 150 input
        minlength  = minimum $ map length fullsearch
        minposs    = filter (\x -> length x == minlength) fullsearch
    print $ length minposs  -- part 1: do fullsearch instead of minposs

main = day17