1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import { loadInput, numericAscSort } from "../common.js";
- const input = loadInput(1, parseInput);
- solve(input);
- function parseInput(inputData) {
-
- const lines = inputData.split(/\n/);
- let leftList = [];
- let rightList = [];
-
- for(const line of lines) {
- const [l, r] = line.split(/\s+/).map((num) => parseInt(num, 10));
- leftList.push(l);
- rightList.push(r);
- }
- leftList.sort(numericAscSort);
- rightList.sort(numericAscSort);
- return [leftList, rightList];
- }
- function solve(parsedInput) {
-
- const [leftList, rightList] = parsedInput;
-
- const deltaList = [];
- while(leftList.length) {
- deltaList.push(Math.abs(rightList.shift() - leftList.shift()));
- }
- console.log("List Distance: ", eval(deltaList.join("+")));
- }
|