this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at c8d3db63099e68d453f8d28e369c6e9d129bd94c 33 lines 1.2 kB view raw
1module Millisecond06 2 ( solve 3 ) where 4 5import Data.List 6import qualified Data.Sequence as S 7 8-- (Part1, Part2) 9solve :: [Int] -> (Int, Int) 10solve banks = go [] $ S.fromList banks 11 where go :: [S.Seq Int] -> S.Seq Int -> (Int, Int) 12 go perms banks' 13 | banks' `elem` perms = case elemIndex banks' perms of 14 Just idx -> (length perms, length perms - idx) 15 Nothing -> (0, 0) 16 | otherwise = let nextStep = redistribute banks' 17 in go (perms ++ [banks']) nextStep 18 19-- Pick biggest bank, reallocate to other banks 20redistribute :: S.Seq Int -> S.Seq Int 21redistribute banks = case maybeMaxIdx of 22 Just idx -> 23 let newBanks = S.update idx 0 banks 24 in go maxBank (idx + 1) newBanks 25 Nothing -> S.empty 26 where maxBank = maximum banks 27 maybeMaxIdx = S.findIndexL (== maxBank) banks 28 go :: Int -> Int -> S.Seq Int -> S.Seq Int 29 go 0 _ banks' = banks' 30 go x idx banks' = 31 if idx == (S.length banks') 32 then go (x - 1) 1 (S.adjust (+1) 0 banks') 33 else go (x - 1) (idx + 1) (S.adjust (+1) idx banks')