-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.js
38 lines (35 loc) · 1.12 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
const fs = require('fs');
solution1 = function(lines) {
let solution = null
lines.forEach(function(firstLine) {
if (solution) return
firstLineN = Number(firstLine)
lines.forEach(function(secondLine) {
secondLineN = Number(secondLine)
if ((firstLineN != secondLineN) &&
(firstLineN + secondLineN == 2020)) solution = firstLineN * secondLineN
})
})
console.log(solution)
}
solution2 = function(lines) {
let solution = null
lines.forEach(function(firstLine) {
if (solution) return
firstLineN = Number(firstLine)
lines.forEach(function(secondLine) {
secondLineN = Number(secondLine)
lines.forEach(function(thirdLine) {
thirdLineN = Number(thirdLine)
if ((firstLineN != secondLineN) && (firstLineN != thirdLineN) && (thirdLineN != secondLineN) &&
(firstLineN + secondLineN + thirdLineN == 2020)) solution = firstLineN * secondLineN * thirdLineN
})
})
})
console.log(solution)
}
fs.readFile('day1.txt', (err, data) => {
if (err) throw err;
solution1(data.toString().split("\n"));
solution2(data.toString().split("\n"));
})