-
Notifications
You must be signed in to change notification settings - Fork 3
/
puzzle.ts
43 lines (43 loc) · 1.12 KB
/
puzzle.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
interface Puzzle {
filepath: string;
input: string;
entries: string[];
blocks: string[][][];
parseInput: (input?: string) => Promise<Puzzle>;
}
export const puzzle: Puzzle = {
filepath: "./input.txt",
input: "",
entries: [],
blocks: [],
parseInput: async function (input?: string) {
if (input) this.input = input;
else {
try {
this.input = await Deno.readTextFile(this.filepath);
} catch (error) {
console.log(error);
return this;
}
}
this.entries = this.input.split("\n");
if (this.entries[this.entries.length - 1] == "") this.entries.pop();
this.blocks = [];
this.entries.forEach((entry, index) => {
if (entry.length == 0 || index == 0) this.blocks.push([]);
if (entry.length == 0) return false;
const separator = [" | ", " -> ", " ", ","]
.filter((s) => entry.split(s).length > 1);
const columns: string[] = [];
if (separator.length > 0) {
entry.split(separator[0])
.filter((column) => column != "")
.forEach((column) => columns.push(column));
} else {
columns.push(entry);
}
this.blocks[this.blocks.length - 1].push(columns);
});
return this;
},
};