-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
71 lines (63 loc) · 1.84 KB
/
util.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
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import path from "path";
export const readInputForDayRaw = async (day: number): Promise<string> => {
const file = Bun.file(path.resolve(`./inputs/p${day}.txt`));
return file.text();
};
export const readInputForDayExampleRaw = async (
day: number,
example?: number
): Promise<string> => {
const file = Bun.file(
path.resolve(`./inputs/p${day}_example${example ? "_" + example : ""}.txt`)
);
return await file.text();
};
export const readInputForDay = async (day: number): Promise<string[]> => {
const file = Bun.file(path.resolve(`./inputs/p${day}.txt`));
return (await file.text()).split("\n");
};
export const readInputForDayExample = async (
day: number,
example?: number
): Promise<string[]> => {
const file = Bun.file(
path.resolve(`./inputs/p${day}_example${example ? "_" + example : ""}.txt`)
);
return (await file.text()).split("\n");
};
function bold(text: string) {
return "\033[1m" + text + "\033[0m";
}
export function printGrid(
grid: (string | number)[][],
title?: string,
marked?: Set<string>,
callback?: (r: number, c: number) => string
) {
let out = "";
let height = 1;
let width = grid[0].length;
if (title) out += title + "\n";
for (let i = 1; i <= width; i++) {
out += " " + i + " ";
}
out += "\n";
out += "┌─" + "──┬─".repeat(width - 1) + "──┐\n";
for (let y = 0; y < grid.length; y++) {
out += "│ ";
for (let x = 0; x < grid[y].length; x++) {
const value =
marked?.has(`${y},${x}`) && callback
? bold(callback(y, x))
: grid[y][x];
out += value + " │ ";
}
out += " " + height + "\n";
height++;
if (y < grid.length - 1) {
out += "├─" + "──┼─".repeat(width - 1) + "──┤\n";
}
}
out += "└─" + "──┴─".repeat(width - 1) + "──┘\n";
console.log(out);
}