Skip to content

Commit ffc1553

Browse files
day 11
1 parent a2a6794 commit ffc1553

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Day11.hs

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@
33
import qualified Data.Attoparsec.Text as At
44
import qualified Data.Map.Strict as M
55
import qualified Data.Set as S
6+
import GHC.Num (integerSizeInBase#)
7+
import GHC.Word (Word(W#))
68
import Control.Monad
79
import Control.Applicative
8-
import GHC.Num (integerSizeInBase#)
9-
import GHC.Word
1010

1111
import Commons
1212

1313
digitCount :: Integer -> Word
1414
digitCount i = W# (integerSizeInBase# (let W# x = 10 in x) i)
1515

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
2121
where d = digitCount n
2222

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+
2327
main :: IO ()
2428
main = do
2529
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)

inputs/11/test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0 1 10 99 999
1+
125 17

0 commit comments

Comments
 (0)