-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
128 lines (108 loc) · 2.88 KB
/
common.ts
File metadata and controls
128 lines (108 loc) · 2.88 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import fs from 'fs/promises';
import path from 'path';
let DAY = 1;
const setDay = (newDay: number) => (DAY = newDay);
const withTestInput = async (
solver: (input: string) => number,
): Promise<number> => {
return solver(
await readInput(path.join(__dirname, `day${DAY}/test_input.txt`)),
);
};
const withRealInput = async (
solver: (input: string) => number,
): Promise<number> => {
return solver(await readInput(path.join(__dirname, `day${DAY}/input.txt`)));
};
const readInput = async (path: string) => {
return await fs.readFile(path, 'utf-8');
};
const inputAsLines = async (path: string): Promise<string[]> => {
try {
return (await fs.readFile(path, 'utf-8')).split('\r\n');
} catch (error) {
console.error(error);
throw error;
}
};
const zip = <T, U>(a: T[], b: U[]): [T, U][] => {
return a.map((elem, index) => [elem, b[index]]);
};
const zipTail = <Type>(a: Type[]) => {
return zip(a.slice(0, a.length - 1), a.slice(1));
};
const increasing = (nums: number[]): boolean => {
return zipTail(nums).every(([first, second]) => second > first);
};
const decreasing = (nums: number[]): boolean => {
return zipTail(nums).every(([first, second]) => first > second);
};
const inBounds = <T>([x, y]: Coord, puzzle: T[][]) => {
return x < puzzle.length && x >= 0 && y >= 0 && y < puzzle[0].length;
};
const asGrid = (input: string): Grid => {
return input.split('\r\n').map((line) => line.split(''));
};
type Coord = [number, number];
type Grid = string[][];
const markedGrid = (grid: Grid, coords: Coord[], symbol: string): string => {
let result = '';
for (let row = 0; row < grid.length; ++row) {
for (let col = 0; col < grid[row].length; ++col) {
if (coords.some(([x, y]) => row === x && col === y)) result += symbol;
else {
result += grid[row][col];
}
}
result += '\n';
}
return result;
};
const toCoords = <T>(grid: T[][]): Coord[] =>
grid.flatMap((row, rowIndex) =>
row.map((_, colIndex): Coord => [rowIndex, colIndex]),
);
const swap = <T>(arr: T[], a: number, b: number) => {
const temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
};
const chunk = <T>(arr: T[], chunkSize: number): T[][] => {
return arr.reduce((acc: T[][], cur, index, _) => {
const chunkIndex = Math.floor(index / chunkSize);
if (!acc[chunkIndex]) {
acc[chunkIndex] = [];
}
acc[chunkIndex].push(cur);
return acc;
}, []);
};
const cardinalSquares = <T>([row, col]: Coord, grid: T[][]): Coord[] => {
const possiblities: Coord[] = [
[row + 1, col],
[row - 1, col],
[row, col + 1],
[row, col - 1],
];
return possiblities.filter((coord) => inBounds(coord, grid));
};
export {
inputAsLines,
readInput,
zip,
zipTail,
increasing,
decreasing,
setDay,
withTestInput,
withRealInput,
Coord,
Grid,
inBounds,
asGrid,
markedGrid,
toCoords,
swap,
chunk,
cardinalSquares,
};