-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.js
53 lines (50 loc) · 1.36 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs')
fs.readFile('./input_day1.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return err;
}
calculateNumbers1(data);
calculateNumbers2(data);
})
function calculateNumbers1(input) {
let found = false;
inputArray = input.split("\n");
for (var i = 0; i < inputArray.length; i++) {
for (var j = 0; j < inputArray.length; j++) {
if (j !== i) {
if (parseInt(inputArray[i]) + parseInt(inputArray[j]) == 2020) {
console.log("Two number result: ", parseInt(inputArray[i])* parseInt(inputArray[j]));
found = true;
break;
}
}
}
if (found == true) {
break;
}
}
}
function calculateNumbers2(input) {
let found = false;
inputArray = input.split("\n");
for (var i = 0; i < inputArray.length; i++) {
for (var j = 0; j < inputArray.length; j++) {
for (var k = 0; k < inputArray.length; k++) {
if (i !== j && i !== k && j !== k) {
if (parseInt(inputArray[i]) + parseInt(inputArray[j]) + parseInt(inputArray[k]) == 2020) {
found = true;
console.log("Three number result: ", parseInt(inputArray[i]) * parseInt(inputArray[j]) * parseInt(inputArray[k]));
break;
}
}
}
if (found == true) {
break;
}
}
if (found == true) {
break;
}
}
}