2015 Day 17 in Haskell
This commit is contained in:
		
							parent
							
								
									80b4cc3cff
								
							
						
					
					
						commit
						44166e07ec
					
				
							
								
								
									
										34
									
								
								2015/day17/day17.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								2015/day17/day17.hs
									
									
									
									
									
										Normal 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
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user