From 44166e07eca1f4d65ceac6992972d300ff477100 Mon Sep 17 00:00:00 2001 From: germax26 Date: Tue, 3 Dec 2024 15:36:56 +1100 Subject: [PATCH] 2015 Day 17 in Haskell --- 2015/day17/day17.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2015/day17/day17.hs diff --git a/2015/day17/day17.hs b/2015/day17/day17.hs new file mode 100644 index 0000000..440dfb3 --- /dev/null +++ b/2015/day17/day17.hs @@ -0,0 +1,34 @@ +import Aoc + +numWays :: Int -> [Int] -> Int +numWays 0 _ = 1 +numWays _ [] = 0 +numWays n (x:xs) + | n > 0 = numWays (n-x) xs + numWays n xs + | otherwise = 0 + +part1 :: [Int] -> Int +part1 = numWays 150 + +numWays' :: Int -> [Int] -> (Int, Int) +numWays' 0 _ = (0, 1) +numWays' _ [] = (0, 0) +numWays' n (x:xs) + | n < 0 = (0, 0) + | w1 == 0 = (m0 + 1, w0) + | w0 == 0 = (m1, w1) + | m0 > m1 -1 = (m1, w1) + | m0 == m1 - 1 = (m1, w0 + w1) + | m0 < m1 -1 = (m0 + 1, w0) + | otherwise = undefined + where + (m0, w0) = numWays' (n-x) xs + (m1, w1) = numWays' n xs + +-- numWays and numWays' can be combined, but I think that that will make it too messy + +part2 :: [Int] -> Int +part2 = snd . numWays' 150 + +main :: IO () +main = aocMain (map read) part1 part2 \ No newline at end of file