-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.js
78 lines (66 loc) · 1.78 KB
/
part2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
let equations = [];
const input = readFileSync(inputFile).toString();
input.split("\n").forEach((line) => {
if (line.split(":").length > 1) {
equations.push({
target: parseInt(line.split(":")[0]),
numbers: line
.split(":")[1]
.trim()
.split(" ")
.map((x) => parseInt(x)),
operators: [],
isValid: false,
});
}
});
function evaluateEquation(equation) {
let result = equation.numbers[0];
equation.numbers.forEach((n, i) => {
if (i != 0) {
let operator = equation.operators[i - 1];
if (operator == "+") {
result += n;
} else if (operator == "*") {
result *= n;
} else {
result = parseInt(`${result}${n}`);
}
}
});
equation.isValid = result == equation.target;
}
function bruteforceEquation(equation) {
let operatorsCount = equation.numbers.length - 1;
let operators = ["+", "*", "||"];
equation.operators = new Array(operatorsCount).fill("+");
while (true) {
evaluateEquation(equation);
if (equation.isValid) break;
for (let i = operatorsCount - 1; i >= 0; i--) {
let currentIndex = operators.indexOf(equation.operators[i]);
if (currentIndex < operators.length - 1) {
equation.operators[i] = operators[currentIndex + 1];
break;
} else {
equation.operators[i] = operators[0];
if (i === 0) return equation;
}
}
}
return equation;
}
let result = 0;
equations.forEach((e, i) => {
let parsed = bruteforceEquation(e);
process.stdout.write(
`Parsed : ${i + 1}/${equations.length}. Current Result : ${result}\r`,
);
if (parsed.isValid) {
result += parsed.target;
}
});
console.log("\n", result);