Skip to content

Commit f51986c

Browse files
committed
Add solution for day 1 - part 2
1 parent 57fb22f commit f51986c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/day-1/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ const distance = leftValues.reduce((sum, left, index) => {
2828
return sum + Math.abs(left - rightValues[index]);
2929
}, 0);
3030

31-
console.log("Part 1: " + distance);
31+
console.log("Part 1: " + distance);
32+
33+
// Calculate similarity score
34+
let similarityScore = 0;
35+
36+
for (const leftValue of leftValues) {
37+
// Count how many times the left value appears in right list
38+
const appearanceInRight = rightValues.filter(rightValue => rightValue === leftValue).length;
39+
40+
// Calculate similarity score
41+
similarityScore += (leftValue * appearanceInRight);
42+
}
43+
44+
console.log("Part 2: " + similarityScore);

0 commit comments

Comments
 (0)