Day 1
This commit is contained in:
37
Day 01/Part 1/index.ts
Normal file
37
Day 01/Part 1/index.ts
Normal file
@@ -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)
|
||||
})
|
||||
}
|
||||
36
Day 01/Part 2/index.ts
Normal file
36
Day 01/Part 2/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
const listOne: number[] = [];
|
||||
const listTwo: number[] = [];
|
||||
const map: Map<number, number> = 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<listTwo.length; i++){
|
||||
let k = listTwo[i] ?? 0;
|
||||
let v = 0;
|
||||
if(map.has(k)) v = map.get(k) ?? 0
|
||||
v++
|
||||
map.set(k, v);
|
||||
}
|
||||
|
||||
let sum = 0
|
||||
|
||||
for(let i = 0; i<listOne.length; i++){
|
||||
let v = listOne[i] ?? 0
|
||||
if(!map.has(v)) continue;
|
||||
let n = v*(map.get(v)??0);
|
||||
|
||||
sum += n;
|
||||
}
|
||||
console.log(sum)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user