From d4cc19c46a1403650f2bae9567558e38b08418f8 Mon Sep 17 00:00:00 2001 From: Alix von Schirp Date: Mon, 9 Dec 2024 06:49:02 +0100 Subject: [PATCH] Day 1 --- Day 01/Part 1/index.ts | 37 +++++++++++++++++++++++++++++++++++++ Day 01/Part 2/index.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Day 01/Part 1/index.ts create mode 100644 Day 01/Part 2/index.ts diff --git a/Day 01/Part 1/index.ts b/Day 01/Part 1/index.ts new file mode 100644 index 0000000..07d2c84 --- /dev/null +++ b/Day 01/Part 1/index.ts @@ -0,0 +1,37 @@ +import fs from 'node:fs'; + +const listOne: number[] = []; +const listTwo: number[] = []; +const listDiff: number[] = []; + +export default function runner(input: string) { + fs.readFile(`./${input}`, 'utf8', (err, data) => { + if (err) throw err; + data.split('\n').forEach(line => { + const spLine = line.split(' '); + if (!spLine[0] || !spLine[1]) throw new Error() + listOne.push(parseInt(spLine[0])) + listTwo.push(parseInt(spLine[1])) + }) + + listOne.sort((a, b) => a - b) + listTwo.sort((a, b) => a - b) + + if (listOne.length != listTwo.length || !listOne || !listTwo) { + throw `Different length of list \n 01: ${listOne.length}, 02: ${listTwo.length}` + } + + for (let i = 0; i < listTwo.length; i++) { + let diff = ((listOne[i] ?? 0) - (listTwo[i] ?? 0)); + if (diff < 0) diff *= -1 + listDiff.push(diff) + } + + let sum = 0; + listDiff.forEach(value => { + sum += value + }) + + console.log(sum) + }) +} \ No newline at end of file diff --git a/Day 01/Part 2/index.ts b/Day 01/Part 2/index.ts new file mode 100644 index 0000000..31204bd --- /dev/null +++ b/Day 01/Part 2/index.ts @@ -0,0 +1,36 @@ +import fs from 'node:fs'; + +const listOne: number[] = []; +const listTwo: number[] = []; +const map: Map = new Map(); + +export default function runner(input: string) { + fs.readFile(`./${input}`, 'utf8', (err, data) => { + if (err) throw err; + data.split('\n').forEach(line => { + const spLine = line.split(' '); + if (!spLine[0] || !spLine[1]) throw new Error() + listOne.push(parseInt(spLine[0])) + listTwo.push(parseInt(spLine[1])) + }) + + for(let i = 0; i