-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.ts
29 lines (23 loc) · 911 Bytes
/
algorithm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { readInputLines, writeOutput, readInputAsGrid, readInputAsNumbers } from "~/utils/io";
// DAY: 1
// PUZZLE: 1
// DATE OF COMPLETION: 2024-12-03
const INPUT_PATH = `./tasks/day01/puzzle1/INPUT`;
const OUTPUT_PATH = `./tasks/day01/puzzle1/OUTPUT`;
(await async function main() {
const lines = await readInputLines(INPUT_PATH)
const pairs = lines
.map((line) => line.split(" "))
.map((pair) => [Number(pair[0]), Number(pair[1])])
const left = pairs.map((pair) => pair[0]).sort((a, b) => a - b)
const right = pairs.map((pair) => pair[1]).sort((a, b) => a - b)
let distances: number[] = [];
for (let i = 0; i < left.length; i++) {
distances.push(Math.abs(left[i] - right[i]))
}
let total: number = 0;
for (let i = 0; i < distances.length; i++) {
total += distances[i]
}
await writeOutput(OUTPUT_PATH, total.toString())
})();