diff options
author | Andreas Grois <andi@grois.info> | 2023-01-05 16:39:30 +0100 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2023-01-05 16:39:30 +0100 |
commit | c8f2eb9ecb95c16a68b3d85c10574a4550b5ceea (patch) | |
tree | 11a655c4f78f34d06068dc76b7f1c2db83a2748b /Day11/app/MonkeyBrain.hs | |
parent | 48fee71319b45f70d2cc807c744dc9fe79fabcfe (diff) |
Day 11: Monkey Brain seems to work now.
Diffstat (limited to 'Day11/app/MonkeyBrain.hs')
-rw-r--r-- | Day11/app/MonkeyBrain.hs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Day11/app/MonkeyBrain.hs b/Day11/app/MonkeyBrain.hs index aaa63a9..86ed6ea 100644 --- a/Day11/app/MonkeyBrain.hs +++ b/Day11/app/MonkeyBrain.hs @@ -136,6 +136,7 @@ tryMakeBinaryExpression v (StringWithMatchingBrackets s) = mapM (makeBinaryExpre fLpRmBoWorker f ((0,_):ss) = fLpRmBoWorker f ss -- first character cannot be a binary operator. fLpRmBoWorker f [] = fst <$> f fLpRmBoWorker f ((i,c):(j,_):ss) | isOperator c && hasSameOrLowerPrecedence (snd <$> f) c && j==i+1 = fLpRmBoWorker (Just (i,c)) ss -- skip the first character right after operator. If it's another operator, it's unary. + fLpRmBoWorker f ((i,c):(j,_):ss) | isOperator c && j==i+1 = fLpRmBoWorker f ss fLpRmBoWorker f ((i,c):ss) | isOperator c && hasSameOrLowerPrecedence (snd <$> f) c = fLpRmBoWorker (Just (i,c)) ss fLpRmBoWorker f (s:ss) = fLpRmBoWorker f ss -- no operator means just go to next char. @@ -145,9 +146,10 @@ tryMakeBinaryExpression v (StringWithMatchingBrackets s) = mapM (makeBinaryExpre hasSameOrLowerPrecedence :: Maybe Char -> Char -> Bool -- this is not typesafe, but well, it's a local helper, so it's probably fine... hasSameOrLowerPrecedence Nothing _ = True hasSameOrLowerPrecedence (Just '/') _ = True - hasSameOrLowerPrecedence (Just '*') c = c == '*' || c == '-' || c == '+' + hasSameOrLowerPrecedence (Just '*') _ = True hasSameOrLowerPrecedence (Just '-') c = c == '-' || c == '+' - hasSameOrLowerPrecedence (Just '+') c = c == '+' + hasSameOrLowerPrecedence (Just '+') c = c == '-' || c == '+' + --tries to make neg expression |