import { loadInput, numericAscSort } from "../common.js";

const input = loadInput(1, parseInput);

solve(input);

/**
 * @param {string} inputData
 * @returns {number[][]}
 */
function parseInput(inputData) {
    // Split the input into lines, and then split each line into two numbers
    const lines = inputData.split(/\n/);
    let leftList = [];
    let rightList = [];

    // split each line into two numbers, then add those to the left and right lists
    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];
}

/**
 * @param {number[][]} parsedInput
 */
function solve(parsedInput) {
    // separate out the already sorted left and right list
    const [leftList, rightList] = parsedInput;
    // Create a list to store the differences between the left and right list
    const deltaList = [];

    while(leftList.length) {
        deltaList.push(Math.abs(rightList.shift() - leftList.shift()));
    }

    console.log("List Distance: ", eval(deltaList.join("+")));
}