2015 Day 17 in Haskell

This commit is contained in:
germax26 2024-12-03 15:36:56 +11:00
parent 80b4cc3cff
commit 44166e07ec
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

34
2015/day17/day17.hs Normal file
View File

@ -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