35 lines
775 B
TypeScript
35 lines
775 B
TypeScript
|
import * as fs from "node:fs/promises";
|
||
|
|
||
|
export async function readFile(filename: string): Promise<number[][]> {
|
||
|
const lines = await fs
|
||
|
.readFile(filename, { encoding: "utf8" })
|
||
|
.then((lines: string) => lines.split(/\n/))
|
||
|
.then((lines: string[]) =>
|
||
|
lines.filter((x) => x).map((x) =>
|
||
|
x.split(/ +/).map((x) => Number(x))
|
||
|
)
|
||
|
);
|
||
|
|
||
|
const l1: number[] = [];
|
||
|
const l2: number[] = [];
|
||
|
|
||
|
lines.forEach(([a, b]) => {
|
||
|
l1.push(a);
|
||
|
l2.push(b);
|
||
|
});
|
||
|
|
||
|
return [l1, l2];
|
||
|
}
|
||
|
|
||
|
export function solve(...lists: number[][]) {
|
||
|
lists.forEach((l) => l.sort());
|
||
|
|
||
|
let sum = 0;
|
||
|
|
||
|
for (const i in lists[0]) {
|
||
|
sum += Math.abs(lists[0][i] - lists[1][i]);
|
||
|
}
|
||
|
|
||
|
return sum;
|
||
|
}
|