-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
48 lines (45 loc) · 1.47 KB
/
utils.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require("fs");
const path = require("path");
/**
* Reads and processes the content of a file.
* Each line is parsed into a pair of integers.
* @param {string} fileName - The name of the file to read.
* @returns {Array<[number, number]>} - An array of integer pairs from the file.
* @throws {Error} - If the file cannot be read or parsed.
*/
const readInputFile = (fileName) => {
try {
const filePath = path.resolve(__dirname, fileName);
const fileContent = fs.readFileSync(filePath, "utf8");
return fileContent
.trim()
.split("\n")
.map((line) => {
const [a, b] = line.split(/\s+/).map(Number);
if (isNaN(a) || isNaN(b)) {
throw new Error(`Invalid data in file: "${line}"`);
}
return [a, b];
});
} catch (error) {
console.error(`Error reading file "${fileName}":`, error.message);
throw error;
}
};
/**
* Combines two arrays into pairs of corresponding elements.
* @param {number[]} leftArray - The first array.
* @param {number[]} rightArray - The second array.
* @returns {Array<[number, number]>} - An array of paired elements.
* @throws {Error} - If arrays are not of the same length.
*/
const zipArrays = (leftArray, rightArray) => {
if (leftArray.length !== rightArray.length) {
throw new Error("Arrays must be of the same length to pair.");
}
return leftArray.map((value, index) => [value, rightArray[index]]);
};
module.exports = {
readInputFile,
zipArrays,
};