-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.js
29 lines (22 loc) · 1.1 KB
/
day10.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
const adaptersJoltagesRatings = require('./entries/day10.json');
adaptersJoltagesRatings.sort((a, b) => a - b);
const builtInAdapterJoltageRating = Math.max.apply(null, adaptersJoltagesRatings) + 3;
const allAdaptersRatings = [0, ...adaptersJoltagesRatings, builtInAdapterJoltageRating];
// PART 1
const ratingsDifferences = allAdaptersRatings.slice(1).reduce((acc, curr, index) => {
const previousRating = adaptersJoltagesRatings[index - 1] || 0;
return [...acc, curr - previousRating];
}, []);
const oneJoltDifferencesCount = ratingsDifferences.filter(v => v === 1).length;
const threeJoltDifferencesCount = ratingsDifferences.filter(v => v === 3).length;
console.log(oneJoltDifferencesCount * threeJoltDifferencesCount);
// PART 2
let pathsCounts = allAdaptersRatings.map((_, i) => (i === 0 ? 1 : 0));
for (let i = 0; i < pathsCounts.length; i++) {
for (let j = i - 3; j < i; j++) {
const ratingsDiff = allAdaptersRatings[i] - allAdaptersRatings[j];
pathsCounts[i] += ratingsDiff <= 3 ? pathsCounts[j] : 0;
}
}
const totalPathsCount = pathsCounts[pathsCounts.length - 1];
console.log(totalPathsCount);