this repo has no description
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')