Created
December 1, 2024 16:09
-
-
Save IMB11/ac8ea30f8e1d4029c31f68d119315396 to your computer and use it in GitHub Desktop.
Advent Of Code 2024 Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Day 1 Part 1 | |
const input = `` | |
const split = input.split("\n"); | |
let list1 = []; | |
let list2 = []; | |
for (let item of split) { | |
let splitItems = item.split(" "); | |
list1.push(splitItems[0]); | |
list2.push(splitItems[1]); | |
} | |
list1.sort(); | |
list2.sort(); | |
let sumOfDifferences = 0; | |
for (let i = 0; i < list1.length; i++) { | |
sumOfDifferences += Math.abs(list1[i] - list2[i]); | |
} | |
console.log("Total distance: " + sumOfDifferences); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Day 1 Part 2 | |
const input = ``; | |
const split = input.split("\n"); | |
let list1 = []; | |
let list2 = []; | |
for (let item of split) { | |
let splitItems = item.split(" "); | |
list1.push(splitItems[0]); | |
list2.push(splitItems[1]); | |
} | |
list1.sort(); | |
list2.sort(); | |
let sumOfSimilarity = 0; | |
for (let i = 0; i < list1.length; i++) { | |
let list1Item = list1[i]; | |
// Determine how many times list1Item occurs in list2 | |
let times = list2.filter(item => item === list1Item).length; | |
sumOfSimilarity += list1Item * times; | |
} | |
console.log("Total similarity: " + sumOfSimilarity); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment