-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.js
56 lines (42 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
54
55
56
const fs = require('fs');
//https://adventofcode.com/2024/day/1
fs.readFile('Day1input.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const firstColumn = [];
const secondColumn = [];
const lines = data.split('\n');
lines.forEach(line => {
const [first, second] = line.split(' ');
if(!isNaN(first) && !isNaN(second)){
firstColumn.push(Number(first));
secondColumn.push(Number(second));
}
});
//firstColumn.sort((a, b) => a - b);
secondColumn.sort((a, b) => a - b);
const similarities = [];
firstColumn.map(
(value, index) => {
similarities.push(countOccurrences(value, secondColumn) * value);
}
);
console.log(similarities);
const result = similarities.reduce((partialSum, a) => partialSum + a, 0);
console.log(result);
function countOccurrences(value, array){
return Number(array.reduce((a, v) => v === value ? a + 1 : a, 0));
}
/* FIRST PART
const distances = [];
for (let i = 0; i < firstColumn.length; i++) {
distances.push(
Math.max(firstColumn[i],secondColumn[i]) - Math.min(firstColumn[i],secondColumn[i])
);
}
const result = distances.reduce((partialSum, a) => partialSum + a, 0);
console.log(result);
*/
});