summaryrefslogtreecommitdiff
path: root/Day11
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2023-01-05 16:39:30 +0100
committerAndreas Grois <andi@grois.info>2023-01-05 16:39:30 +0100
commitc8f2eb9ecb95c16a68b3d85c10574a4550b5ceea (patch)
tree11a655c4f78f34d06068dc76b7f1c2db83a2748b /Day11
parent48fee71319b45f70d2cc807c744dc9fe79fabcfe (diff)
Day 11: Monkey Brain seems to work now.
Diffstat (limited to 'Day11')
-rw-r--r--Day11/app/MonkeyBrain.hs6
-rw-r--r--Day11/tests/MonkeyBrain-Tests.hs20
2 files changed, 22 insertions, 4 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
diff --git a/Day11/tests/MonkeyBrain-Tests.hs b/Day11/tests/MonkeyBrain-Tests.hs
index 9049aeb..4f8fd9f 100644
--- a/Day11/tests/MonkeyBrain-Tests.hs
+++ b/Day11/tests/MonkeyBrain-Tests.hs
@@ -7,9 +7,25 @@ import MonkeyBrain
main = defaultMain tests
-test1 = TestCase $ assertEqual "(2+7*3+2-1-3+17/4*2)*(3+4*-1+20)/(17-3*4)"
+testMulDivPrecedence1 = TestCase $ assertEqual "(2+7*3+2-1-3+17/4*2)*(3+4*-1+20)/(17-3*4)"
(Right (Just (((2+7*3+2-1-3+17 `quot` 4*2)*(3+4*(-1)+20) `quot` (17-3*4)) :: Int)))
$ fmap (\x -> evaluateIntExpression x (VariableValues [])) (tryParseExpression (VariableNameValidator (const True)) "(2+7*3+2-1-3+17/4*2)*(3+4*-1+20)/(17-3*4)")
+testSubDivChain1 = TestCase $ assertEqual "2+3-1+4"
+ (Right (Just ((2+3-1+4) :: Int)))
+ $ fmap (\x -> evaluateIntExpression x (VariableValues [])) (tryParseExpression (VariableNameValidator (const True)) "2+3-1+4")
+
+testABitMoreComplex1 = TestCase $ assertEqual "17+37*2-(15-7/2*(5-1))*2+-(32-4*3)/5*2+33"
+ (Right (Just ((17+37*2-(15-7 `quot` 2*(5-1))*2+(-(32-4*3)) `quot` 5*2+33) :: Int)))
+ $ fmap (\x -> evaluateIntExpression x (VariableValues [])) (tryParseExpression (VariableNameValidator (const True)) "17+37*2-(15-7/2*(5-1))*2+-(32-4*3)/5*2+33")
+
+testWithVariables = TestCase $ assertEqual "12+8*-a+37/b-(5-c*3), a=2, b=8, c=-1"
+ (Right (Just ((12+8*(-2)+37 `quot` 8-(5-(-1)*3)) :: Int)))
+ $ fmap (\x -> evaluateIntExpression x (VariableValues [(Variable "a", 2), (Variable "b", 8), (Variable "c", -1)]))
+ (tryParseExpression (VariableNameValidator (const True)) "12+8*-a+37/b-(5-c*3)")
+
-- hUnitTestToTests: Adapt an existing HUnit test into a list of test-framework tests
-tests = hUnitTestToTests $ TestList [TestLabel "Test1" test1] \ No newline at end of file
+tests = hUnitTestToTests $ TestList [TestLabel "Operator Precedence, Mul, Div 1" testMulDivPrecedence1,
+ TestLabel "Simple Addition Subtraction Chain" testSubDivChain1,
+ TestLabel "A bit more complex formula" testABitMoreComplex1,
+ TestLabel "With three Variables" testWithVariables] \ No newline at end of file