--- 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
part2 rules = sum $ fmap registerRange accepted
where accepted = evaluateRules rules
- (Evaluation [] [(WaitingPart "in" initialPart)])
+ (Evaluation [] [WaitingPart "in" initialPart])
applyWorkflow :: String -> RuleBase -> Part Int -> 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
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)
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)
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
)
-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