~rootmos/AoC

355be4902cee6937a844ce479f52f70fa458d142 — Gustav Behm a month ago d7fc663
Add solution to AoC 2023 #08b
1 files changed, 4 insertions(+), 3 deletions(-)

M 2023/08b.hs
M 2023/08b.hs => 2023/08b.hs +4 -3
@@ 6,6 6,7 @@
module Main where

import Qulude
import Qulude.Prime ( factorize )

--import Data.Array ( (!) )
import qualified Data.Array as Array


@@ 65,14 66,14 @@ findPrefixAndCycle w = let (i, vs) = f [] Map.empty w in
            Nothing -> f (w:acc) (Map.insert w i vs) ws

analyze (p, cs) = case (f p, f cs) of
                    ((l, []), (c, e:[])) -> if e + l == c then (l, c, e) else undefined
                    ((p, []), (c, e:[])) -> if p + e == c then (p, c, e) else undefined
  where f w = (length w, List.findIndices isEnd w)

partB (is, map) = fmap f starts
partB (is, map) = foldl (*) 1 $ foldl List.union [] $ fmap factorize $ fmap (\(_, c, _) -> c) $ fmap f starts
  where starts = filter isStart $ Map.keys map
        f s = analyze $ findPrefixAndCycle $ walk map is s

main :: IO ()
main = with parser $ do
  --run "08b.example" "b example" partB -- 6
  run "08.input" "b input" partB
  run "08.input" "b input" partB -- 21003205388413