Day 10 - part 2
yeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
This commit is contained in:
parent
a1a4a6fb52
commit
d720534eaf
1 changed files with 21 additions and 11 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue