From 051f973b9fa29584af6d29f3662bc504f956dbd2 Mon Sep 17 00:00:00 2001 From: jle Date: Fri, 15 Dec 2023 10:20:03 -0800 Subject: [PATCH] clean up day 15 a bit --- src/AOC/Challenge/Day15.hs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/AOC/Challenge/Day15.hs b/src/AOC/Challenge/Day15.hs index d8d08a4..b95e2bb 100644 --- a/src/AOC/Challenge/Day15.hs +++ b/src/AOC/Challenge/Day15.hs @@ -116,11 +116,14 @@ setBox label n b initBox :: String -> Int -> Box initBox label n = B (NEM.singleton label (BN n Nothing Nothing)) label label -traceBox :: Box -> [(String, Int)] -traceBox B {..} = go _bFirst +boxSum :: Box -> Int +boxSum B {..} = go 1 0 _bFirst where - go x = (x, _bnValue) : maybe [] go _bnAfter + go !i !n x = case _bnAfter of + Nothing -> n' + Just y -> go (i + 1) n' y where + n' = n + _bnValue * i BN {..} = _bMap NEM.! x day15b :: [(String, Act)] :~> Int @@ -138,8 +141,4 @@ day15b = Set n -> IM.alter (Just . maybe (initBox lbl n) (setBox lbl n)) i mp where i = hasher lbl + 1 - - score boxNum = - sum - . zipWith (\i (_, n) -> boxNum * i * n) [1 ..] - . traceBox + score boxNum box = boxNum * boxSum box