0
0
Fork 0
AdventOfCode/day_01/javascript/part1.mjs
2024-12-01 15:49:08 -07:00

31 lines
742 B
JavaScript

import { readFile } from "fs/promises";
import { SortedList } from "../../utils/javascript/SortedList.mjs";
const fileToRead = `actual.input`;
const list1 = new SortedList();
const list2 = new SortedList();
const file = await readFile(fileToRead, `utf-8`);
for (const line of file.split(`\n`)) {
// console.log(line);
const [ l1Add, l2Add ] = line.split(/\s+/, 2);
list1.add(l1Add);
list2.add(l2Add);
};
if (list1.size !== list2.size) {
console.log(`The two lists are different lengths`);
process.exit(1);
};
const size = list1.size;
let distance = 0;
for (var i = 0; i < size; i++) {
const place1 = list1.at(i);
const place2 = list2.at(i);
distance += Math.abs(place2 - place1);
}
console.log(`Total Distance: ${distance}`);