-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb16637.js
More file actions
49 lines (41 loc) · 961 Bytes
/
b16637.js
File metadata and controls
49 lines (41 loc) · 961 Bytes
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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
let max = -Infinity;
const exp = splitIntoChunk(input[1]);
function dfs(result, i) {
if (i >= exp.length) {
max = Math.max(result, max);
return;
}
dfs(calc(exp[i], result, exp[i + 1]), i + 2);
if (i < exp.length - 3) {
dfs(calc(exp[i], result, calc(exp[i + 2], exp[i + 1], exp[i + 3])), i + 4);
}
}
function calc(op, a, b) {
switch (op) {
case "+":
return Number(a) + Number(b);
case "-":
return Number(a) - Number(b);
default:
return Number(a) * Number(b);
}
}
function splitIntoChunk(exp) {
let acc = "";
const result = [];
for (let i = 0; i < exp.length; i++) {
if (["+", "-", "*"].includes(exp[i])) {
result.push(acc);
result.push(exp[i]);
acc = "";
} else {
acc += exp[i];
}
}
result.push(acc);
return result;
}
dfs(+exp[0], 1);
console.log(max);