From d720534eaf3e5eb860bf16e6e9f356dd3208051c Mon Sep 17 00:00:00 2001 From: Martin Potier Date: Fri, 11 Dec 2020 09:30:17 +0200 Subject: [PATCH] Day 10 - part 2 yeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah --- day10/main.hs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/day10/main.hs b/day10/main.hs index cf2a7bd..c3e17d0 100755 --- a/day10/main.hs +++ b/day10/main.hs @@ -32,24 +32,34 @@ solvePart1 xs = finally -------------------------------------------------------------------------------- -- 1 -> 1 possibility --- 1 1 -> 2 poss --- 1 1 1 -> 4 poss --- 1 1 1 1 -> 6 poss --- 1 1 1 1 1 -> 8 poss +-- 1 1 -> 2 possibilities +-- 1 1 1 -> 4 possibilities +-- 1 1 1 1 -> 7 (!) possibilities +-- 1 1 1 1 1 -> ? possibilities -- -------------------------------------------------------------------------------- cleanup :: [[Int]] -> [[Int]] cleanup = filter (\v -> (any (/= 3) v) && (length v > 1)) -est :: [[Int]] -> [Int] -est = map combinations + +-- That is the actual problem right here: +-- ··· -> 2^3 - 1 = 7 +-- ···· -> 2^4 - 1 - 2 = 13 +-- ····· -> 2^5 - 1 - 2 - 3 = 26 +combinations :: [Int] -> Int +combinations v = + (2 ^ n) -- All the ways to take any on/off combination in n + - (sum [1..(n-2)]) -- All the ways to take n+ consecutive in n (illegal), + -- *except* for n=1 and n=2 which are legal where - combinations v = (2 ^ l) - (2 ^ (l `div` 3)) - where - l = (length v) - 1 + n = (length v) - 1 estimatePermutations :: [Int] -> Int -estimatePermutations = getProduct . foldMap Product . est . cleanup . group +estimatePermutations = getProduct + . foldMap Product + . map combinations + . cleanup + . group distanceMap :: [Int] -> [Int] distanceMap xs = map (\(x,y) -> y - x) $ zip (0:sorted) (sorted ++ [(last sorted)+3]) @@ -77,6 +87,6 @@ main = do print $ distanceMap smallExample print $ solvePart2 smallExample print $ distanceMap biggerExample - print $ (est . cleanup . group) $ distanceMap biggerExample + print $ (map combinations . cleanup . group) $ distanceMap biggerExample print $ solvePart2 biggerExample print $ solvePart2 input