|
3 | 3 | import qualified Data.Attoparsec.Text as At
|
4 | 4 | import qualified Data.Map.Strict as M
|
5 | 5 | import qualified Data.Set as S
|
| 6 | +import GHC.Num (integerSizeInBase#) |
| 7 | +import GHC.Word (Word(W#)) |
6 | 8 | import Control.Monad
|
7 | 9 | import Control.Applicative
|
8 |
| -import GHC.Num (integerSizeInBase#) |
9 |
| -import GHC.Word |
10 | 10 |
|
11 | 11 | import Commons
|
12 | 12 |
|
13 | 13 | digitCount :: Integer -> Word
|
14 | 14 | digitCount i = W# (integerSizeInBase# (let W# x = 10 in x) i)
|
15 | 15 |
|
16 |
| -step :: Integer -> S.Set Integer |
17 |
| -step 0 = S.singleton 1 |
18 |
| -step n |
19 |
| - | even d = S.fromList [n `mod` (10 ^ div d 2), n `div` (10 ^ div d 2)] |
20 |
| - | otherwise = S.singleton (n * 2024) |
| 16 | +step :: Integer -> Integer -> M.Map Integer Integer |
| 17 | +step 0 c = M.singleton 1 c |
| 18 | +step n c |
| 19 | + | even d = M.fromListWith (+) [(n `mod` (10 ^ div d 2), c), (n `div` (10 ^ div d 2), c)] |
| 20 | + | otherwise = M.singleton (n * 2024) c |
21 | 21 | where d = digitCount n
|
22 | 22 |
|
| 23 | +turn :: M.Map Integer Integer -> M.Map Integer Integer |
| 24 | +turn m = M.unionsWith (+) nextStep |
| 25 | + where nextStep = map (uncurry step) $ M.assocs m |
| 26 | + |
23 | 27 | main :: IO ()
|
24 | 28 | main = do
|
25 | 29 | i <- inp (At.sepBy1 At.decimal At.space)
|
26 |
| - let stepG = foldr (\cu ac -> step cu `S.union` ac) S.empty |
27 |
| - -- TODO cool! this reaches steady state at 3811 elements |
28 |
| - mapM_ (print . S.size) $ take 150 $ iterate stepG $ S.fromList i |
| 30 | + let start = M.fromList $ map (,1) i |
| 31 | + score = sum . M.elems |
| 32 | + print $ score (iterate turn start !! 25) |
| 33 | + print $ score (iterate turn start !! 75) |
0 commit comments