Restructure folders and add day 3's solution
This commit is contained in:
parent
ecab4f263a
commit
519c1b63cc
6 changed files with 123 additions and 1 deletions
26
javascript/day_01/part2.mjs
Normal file
26
javascript/day_01/part2.mjs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { readFile } from "fs/promises";
|
||||
|
||||
|
||||
const fileToRead = `actual.input`;
|
||||
|
||||
const list1 = {};
|
||||
const list2 = {};
|
||||
|
||||
const file = await readFile(fileToRead, `utf-8`);
|
||||
|
||||
for (const line of file.split(`\n`)) {
|
||||
const [ l1Add, l2Add ] = line.split(/\s+/, 2);
|
||||
|
||||
list1[l1Add] ??= 0;
|
||||
list1[l1Add] += 1;
|
||||
|
||||
list2[l2Add] ??= 0;
|
||||
list2[l2Add] += 1;
|
||||
};
|
||||
|
||||
let similarity = 0;
|
||||
for (const location in list1) {
|
||||
similarity += list1[location] * (location * (list2[location] ?? 0))
|
||||
};
|
||||
|
||||
console.log(`Similarity: ${similarity}`);
|
||||
Loading…
Add table
Add a link
Reference in a new issue