Created
December 2, 2024 09:28
-
-
Save kirkegaard/16848b06376d47f4ea1ca2b58b04f028 to your computer and use it in GitHub Desktop.
advent of code day 1
This file contains 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
const input = await Deno.readTextFile("input.txt"); | |
const data = input.split("\n"); | |
let left: number[] = []; | |
let right: number[] = []; | |
for (const pair of data) { | |
const [a, b] = pair.split(" "); | |
left.push(+a); | |
right.push(+b); | |
} | |
left = left.sort((a, b) => a - b); | |
right = right.sort((a, b) => a - b); | |
const dist = left.reduce((acc, val, idx) => { | |
return acc + Math.abs(val - right[idx]) || acc; | |
}, 0); | |
console.log({ answer1: dist }); | |
const dist2: number[] = []; | |
left.forEach((val) => { | |
const contains = right.filter((v) => v === val); | |
dist2.push(contains.length * val); | |
}); | |
const total = dist2.reduce((acc, val) => acc + val || acc, 0); | |
console.log({ answer2: total }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment