-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.mjs
56 lines (46 loc) · 1.22 KB
/
common.mjs
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
49
50
51
52
53
54
55
56
import { readFile } from 'fs/promises';
export async function readLines(path) {
try {
const inputD = await readFile(path);
return inputD.toString().split('\n');
} catch (err) {
throw err;
}
}
export function printd(str, debug_mode) {
if (debug_mode) {
console.log(str);
}
}
export function printdMap(map, debug_mode) {
if (debug_mode) {
const width = map[0].length;
let tensHeader = " "; // 5 spaces for line lables
let unitHeader = " ";
for (let i = 0; i <= Math.floor(width / 10); i++) {
tensHeader += `${i} `;
for (let j = 0; j < 10; j++) {
unitHeader += `${j}`;
}
}
console.log(tensHeader);
console.log(unitHeader);
for (let i = 0; i < map.length; i++) {
console.log(`${String(i).padStart(4, ' ')} ${map[i].join('')}`)
}
}
}
/**
* @param {string[]} lines lines read one by one from a file
* @returns {string[][]}
*/
export function buildMapMatrix(lines) {
return lines.filter(l => l.trim() !== "").map(l => l.split(''));
}
/**
* @param {string[]} lines lines read one by one from a file
* @returns {number[][]}
*/
export function buildNumberMapMatrix(lines) {
return lines.filter(l => l.trim() !== "").map(l => l.split('')).map(r => r.map(c => Number(c)));
}