From 1945dfe5e07195d9988073ef884838e47007d068 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 24 Dec 2023 11:39:06 +0000 Subject: [PATCH] Updated blog link --- advent19/Main.hs | 55 +++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/advent19/Main.hs b/advent19/Main.hs index ff4187c..eed9e35 100644 --- a/advent19/Main.hs +++ b/advent19/Main.hs @@ -1,4 +1,4 @@ --- Writeup at https://work.njae.me.uk/2023/12/22/advent-of-code-2023-day-18/ +-- Writeup at https://work.njae.me.uk/2023/12/24/advent-of-code-2023-day-19/ import AoC @@ -78,7 +78,7 @@ part1 rules parts = sum $ fmap sumRegisters acceptedParts part2 rules = sum $ fmap registerRange accepted where accepted = evaluateRules rules - (Evaluation [] [(WaitingPart "in" initialPart)]) + (Evaluation [] [WaitingPart "in" initialPart]) applyWorkflow :: String -> RuleBase -> Part Int -> Destination @@ -99,13 +99,16 @@ applyElement :: Part Int -> RuleElement -> Destination applyElement _ (WithoutTest dest) = dest applyElement part (WithTest test dest) | (test ^. comparator == Lt) = - if (regValue part (test ^. register) < (test ^. threshold)) + -- if (regValue part (test ^. register) < (test ^. threshold)) + if part ^. l < test ^. threshold then dest else Continue | otherwise = - if (regValue part (test ^. register) > (test ^. threshold)) + -- if (regValue part (test ^. register) > (test ^. threshold)) + if part ^. l > test ^. threshold then dest else Continue + where l = lensOfR (test ^. register) regValue :: Part a -> Register -> a regValue part X = part ^. x @@ -114,11 +117,11 @@ regValue part A = part ^. a regValue part S = part ^. s --- lensOfR :: Register -> Lens' (Part a) a --- lensOfR X = x --- lensOfR M = m --- lensOfR A = a --- lensOfR S = s +lensOfR :: Register -> Lens' (Part a) a +lensOfR X = x +lensOfR M = m +lensOfR A = a +lensOfR S = s sumRegisters :: Part Int -> Int sumRegisters part = (part ^. x) + (part ^. m) + (part ^. a) + (part ^. s) @@ -141,15 +144,15 @@ evaluateRules rules (Evaluation accepted ((WaitingPart rulename part):waiting)) where rulebody = rules ! rulename newEvaluation = applyRuleI part rulebody -applyRuleI :: (Part Interval) -> [RuleElement] -> Evaluation -applyRuleI part [] = mempty -applyRuleI part (x:xs) = +applyRuleI :: Part Interval -> [RuleElement] -> Evaluation +applyRuleI _ [] = mempty +applyRuleI part (e:es) = case inProgress of Nothing -> evaluation - Just p -> evaluation <> (applyRuleI p xs) - where (evaluation, inProgress) = applyElementI part x + Just p -> evaluation <> (applyRuleI p es) + where (evaluation, inProgress) = applyElementI part e -applyElementI :: (Part Interval) -> RuleElement -> (Evaluation, Maybe (Part Interval)) +applyElementI :: Part Interval -> RuleElement -> (Evaluation, Maybe (Part Interval)) applyElementI part (WithoutTest Accept) = (mempty & accepted .~ [part], Nothing) applyElementI part (WithoutTest Reject) = (mempty, Nothing) applyElementI part (WithoutTest (Rule rule)) = (mempty & waiting .~ [WaitingPart rule part], Nothing) @@ -161,18 +164,18 @@ applyElementI part (WithTest test dest) = (evaluation, failing) splitPart :: Part Interval -> Test -> (Maybe (Part Interval), Maybe (Part Interval)) splitPart part test = (passingPart, failingPart) - where -- regLens = lensOfR $ test ^. register + where l = lensOfR (test ^. register) :: Lens' (Part Interval) Interval (passingInterval, failingInterval) = - -- splitInterval (part ^. regLens) (test ^. comparator) (test ^. threshold) + -- splitInterval (part ^. l) (test ^. comparator) (test ^. threshold) splitInterval (regValue part (test ^. register)) (test ^. comparator) (test ^. threshold) passingPart = case passingInterval of Nothing -> Nothing - -- Just interval -> Just (part & regLens .~ interval) - Just interval -> Just (setRegister part test interval) + Just pi -> Just (part & l .~ pi) + -- Just interval -> Just (setRegister part test interval) failingPart = case failingInterval of Nothing -> Nothing - -- Just interval -> Just (part & regLens .~ interval) - Just interval -> Just (setRegister part test interval) + Just fi -> Just (part & l .~ fi) + -- Just interval -> Just (setRegister part test interval) splitInterval :: Interval -> Comparator -> Int -> (Maybe Interval, Maybe Interval) splitInterval interval Lt threshold @@ -189,11 +192,11 @@ splitInterval interval Gt threshold ) -setRegister :: Part Interval -> Test -> Interval -> Part Interval -setRegister part (Test X _ _) val = part & x .~ val -setRegister part (Test M _ _) val = part & m .~ val -setRegister part (Test A _ _) val = part & a .~ val -setRegister part (Test S _ _) val = part & s .~ val +-- setRegister :: Part Interval -> Test -> Interval -> Part Interval +-- setRegister part (Test X _ _) val = part & x .~ val +-- setRegister part (Test M _ _) val = part & m .~ val +-- setRegister part (Test A _ _) val = part & a .~ val +-- setRegister part (Test S _ _) val = part & s .~ val -- Parse the input file -- 2.34.1