-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.js
38 lines (34 loc) · 894 Bytes
/
day1.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
const fs = require('fs');
const readInput = () => {
try {
const data = fs.readFileSync('day1Input.txt', 'UTF-8');
const lines = data.split(/\r?\n/);
return lines.map(a => parseInt(a));
} catch (err) {
console.error(err);
}
}
const input = readInput().sort((a, b) => a - b);
let i = 0;
let found = false;
while (i < input.length && !found) {
let j = input.length - 1;
while (i < j) {
let k = input.length - 1;
while (k > j) {
let sum = input[i] + input[j] + input[k];
if (sum === 2020) {
console.log(input[i], input[j], input[k]);
console.log(input[i] * input[j] * input[k]);
found = true;
break;
}
if (sum < 2020) {
break;
}
k--;
}
j--;
}
i++;
}