-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7a.js
24 lines (20 loc) · 785 Bytes
/
7a.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
import {readInput} from "./lib.js";
const input = readInput(7).map((line) => line.split(" ").map(x => parseInt(x)));
const answer = input
.filter(([test, ...ops]) => {
return combination(['+', '*'], ops.length).some((comb) => {
const result = ops.reduce((acc, op, i) => {
if (comb[i] === '+') return acc + op;
if (comb[i] === '*') return acc * op;
});
return result === test;
});
})
.map(([test]) => test)
.reduce((acc, test) => acc + test, 0);
function combination(parts,N){
const parts_normalize = []
while (N--) parts_normalize.push(parts)
return parts_normalize.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
}
console.log(answer);