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 |
|
16 |
1.1 |
|
21 |
1.1 |
|
26 |
1.1 |
|
33 |
1.1 |
|
39 |
1.1 |
|
44 |
1.1 2.2 |
|
50 |
1.1 |
|
57 |
1.1 2.2 |
|
58 |
1.1 |
|
63 |
1.1 |
|
68 |
1.1 |
|
73 |
1.1 |
|
76 |
1.1 2.2 |
|
78 |
1.1 2.2 |
|
80 |
1.1 |
|
96 |
1.1 |