Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
clean up day 15 a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mstksg committed Dec 15, 2023
1 parent aadf1b0 commit 051f973
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/AOC/Challenge/Day15.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 051f973

Please sign in to comment.