-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.js
44 lines (42 loc) · 836 Bytes
/
01.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
/**
* @param {string} d
*/
const part1 = async d => {
const data = d.split('\n').map(e => parseInt(e)).sort((a, b) => a - b);
for (let i = 0, li = data.length; i < li; i++) {
for (let j = i + 1; j < li; j++) {
const t = data[i] + data[j];
if (t > 2020) {
break;
}
if (t == 2020) {
return data[i] * data[j];
}
}
}
return null;
};
/**
* @param {string} d
*/
const part2 = async d => {
const data = d.split('\n').map(e => parseInt(e)).sort((a, b) => a - b);
for (let i = 0, li = data.length; i < li; i++) {
for (let j = i + 1; j < li; j++) {
for (let k = j + 1; k < li; k ++) {
const t = data[i] + data[j] + data[k];
if (t > 2020) {
break;
}
if (t == 2020) {
return data[i] * data[j] * data[k];
}
}
}
}
return null;
};
module.exports = {
part1,
part2
};