Calculator.kt

1
package com.adventofcode.day18
2
3
class Calculator(private val expressions: List<String>) {
4
    constructor(expression: String) : this(listOf(expression))
5
6
7
    fun evaluate(): Long {
8
        return expressions.map {
9
            evaluate(it)
10 1 1. evaluate : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluate → KILLED
        }.sum()
11
12
    }
13
14
    private fun evaluate(expression: String): Long {
15
        val reducedExpression = reduceParenthesis(expression)
16 1 1. evaluate : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluate → KILLED
        return executeSimply(reducedExpression)
17
    }
18
19
    private fun reduceParenthesis(expression: String): String {
20
        var reducedExpression = expression
21 1 1. reduceParenthesis : negated conditional → TIMED_OUT
        while (reducedExpression.contains("(")) { // soppose that expression is well formed then Nb of ( == nb of )
22
            val inParenthesisExpression = reducedExpression.substringAfterLast("(").substringBefore(")")
23
            val inParenthesisResult = executeSimply(inParenthesisExpression).toString()
24
            reducedExpression = reducedExpression.replace("($inParenthesisExpression)", inParenthesisResult)
25
        }
26 1 1. reduceParenthesis : replaced return value with "" for com/adventofcode/day18/Calculator::reduceParenthesis → KILLED
        return reducedExpression
27
    }
28
29
    private fun executeSimply(data: String): Long {
30
        val split = data.split(" ").iterator()
31
        var m1 = split.next().toLong()
32
        var res = 0L
33 1 1. executeSimply : negated conditional → KILLED
        while (split.hasNext()) {
34
            val op = split.next()
35
            val m2 = split.next()
36
            res = evaluateExpression(m1, op, m2.toLong())
37
            m1 = res
38
        }
39 1 1. executeSimply : replaced long return with 0 for com/adventofcode/day18/Calculator::executeSimply → KILLED
        return res
40
    }
41
42
    private fun evaluateExpression(m1: Long, op: String, m2: Long): Long {
43
        if (op == "+") return (m1 + m2)
44 2 1. evaluateExpression : Replaced long multiplication with division → KILLED
2. evaluateExpression : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpression → KILLED
        return (m1 * m2)
45
    }
46
47
    fun evaluateWithPrecedence(): Long {
48
        return expressions.map {
49
            evaluateExpressionWithPrecedence(it)
50 1 1. evaluateWithPrecedence : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateWithPrecedence → KILLED
        }.sum()
51
52
    }
53
54
    private fun evaluateExpressionWithPrecedence(expression: String): Long {
55
        val toto = reduceParenthesisv2(expression)
56
        val reducedExpression = reduceAddition(toto)
57 2 1. evaluateExpressionWithPrecedence : negated conditional → KILLED
2. evaluateExpressionWithPrecedence : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpressionWithPrecedence → KILLED
        if (reducedExpression.contains(" ")) return executeSimply(reducedExpression)
58 1 1. evaluateExpressionWithPrecedence : replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpressionWithPrecedence → KILLED
        return reducedExpression.toLong()
59
    }
60
61
    private fun reduceParenthesisv2(expression: String): String {
62
        var reducedExpression = expression
63 1 1. reduceParenthesisv2 : negated conditional → KILLED
        while (reducedExpression.contains("(")) { // soppose that expression is well formed then Nb of ( == nb of )
64
            val inParenthesisExpression = reducedExpression.substringAfterLast("(").substringBefore(")")
65
            val inParenthesisResult = evaluateExpressionWithPrecedence(inParenthesisExpression).toString()
66
            reducedExpression = reducedExpression.replace("($inParenthesisExpression)", inParenthesisResult)
67
        }
68 1 1. reduceParenthesisv2 : replaced return value with "" for com/adventofcode/day18/Calculator::reduceParenthesisv2 → KILLED
        return reducedExpression
69
    }
70
71
    private fun reduceAddition(expression: String): String {
72
        var reducedExpression = expression
73 1 1. reduceAddition : negated conditional → KILLED
        while (reducedExpression.contains("+")) {
74
            val data = reducedExpression.split(" ")
75
            val index = data.indexOfFirst { it == "+" }
76 2 1. reduceAddition : Replaced integer subtraction with addition → KILLED
2. reduceAddition : Replaced integer addition with subtraction → KILLED
            val addition = data.subList(index - 1, index + 2).joinToString(" ")
77
            val additionResult = executeSimply(addition).toString()
78 2 1. reduceAddition : Replaced integer subtraction with addition → TIMED_OUT
2. reduceAddition : Replaced integer addition with subtraction → KILLED
            reducedExpression = data.subList(0, index - 1).plus(additionResult).plus(data.subList(index + 2, data.size)).joinToString(" ")
79
        }
80 1 1. reduceAddition : replaced return value with "" for com/adventofcode/day18/Calculator::reduceAddition → KILLED
        return reducedExpression
81
    }
82
}

Mutations

10

1.1
Location : evaluate
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluate → KILLED

16

1.1
Location : evaluate
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluate → KILLED

21

1.1
Location : reduceParenthesis
Killed by : none
negated conditional → TIMED_OUT

26

1.1
Location : reduceParenthesis
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
replaced return value with "" for com/adventofcode/day18/Calculator::reduceParenthesis → KILLED

33

1.1
Location : executeSimply
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
negated conditional → KILLED

39

1.1
Location : executeSimply
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
replaced long return with 0 for com/adventofcode/day18/Calculator::executeSimply → KILLED

44

1.1
Location : evaluateExpression
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
Replaced long multiplication with division → KILLED

2.2
Location : evaluateExpression
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[method:shouldApplysAdditionAndMultiplicationWithSamePrecedence()]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpression → KILLED

50

1.1
Location : evaluateWithPrecedence
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateWithPrecedence → KILLED

57

1.1
Location : evaluateExpressionWithPrecedence
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
negated conditional → KILLED

2.2
Location : evaluateExpressionWithPrecedence
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#6]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpressionWithPrecedence → KILLED

58

1.1
Location : evaluateExpressionWithPrecedence
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
replaced long return with 0 for com/adventofcode/day18/Calculator::evaluateExpressionWithPrecedence → KILLED

63

1.1
Location : reduceParenthesisv2
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
negated conditional → KILLED

68

1.1
Location : reduceParenthesisv2
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
replaced return value with "" for com/adventofcode/day18/Calculator::reduceParenthesisv2 → KILLED

73

1.1
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#6]
negated conditional → KILLED

76

1.1
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
Replaced integer subtraction with addition → KILLED

2.2
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
Replaced integer addition with subtraction → KILLED

78

1.1
Location : reduceAddition
Killed by : none
Replaced integer subtraction with addition → TIMED_OUT

2.2
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
Replaced integer addition with subtraction → KILLED

80

1.1
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
replaced return value with "" for com/adventofcode/day18/Calculator::reduceAddition → KILLED

96

1.1
Location : reduceAddition
Killed by : com.adventofcode.day18.CalculatorTest.[engine:junit-jupiter]/[class:com.adventofcode.day18.CalculatorTest]/[test-factory:shouldApplysAdditionAndMultiplicationWithDeclaredPrecedence()]/[dynamic-test:#5]
Changed increment from 1 to -1 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.6.1