-
Notifications
You must be signed in to change notification settings - Fork 0
/
ten.ts
132 lines (115 loc) · 2.96 KB
/
ten.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
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
129
130
131
132
const getMatrix = (str: string) =>
str
.trim()
.split("\n")
.map((row) => row.split(""));
const findStart = (mx: string[][]) => {
let start: [number, number] = [0, 0];
mx.forEach((row, r) =>
row.forEach((letr, l) => {
if (letr === "S") start = [r, l];
})
);
return start;
};
const getCell = (matrix: string[][], p: [number, number]) => {
const [x, y] = p;
if (p.includes(-1)) return null;
return matrix[y][x];
};
const connectTop = (mx: string[][], pos: [number, number]) => {
const [x, y] = pos;
const nb = [x, y - 1];
const shape = getCell(mx, nb);
if (shape === null) return null;
const connects = "7|F".includes(shape);
return connects ? nb : null;
};
const connectBot = (mx: string[][], pos: [number, number]) => {
const [x, y] = pos;
const nb = [x, y + 1];
const shape = getCell(mx, nb);
if (shape === null) return null;
const connects = "J|L".includes(shape);
return connects ? nb : null;
};
const connectLeft = (mx: string[][], pos: [number, number]) => {
const [x, y] = pos;
const nb = [x - 1, y];
const shape = getCell(mx, nb);
if (shape === null) return null;
const connects = "F-L".includes(shape);
return connects ? nb : null;
};
const connectRight = (mx: string[][], pos: [number, number]) => {
const [x, y] = pos;
const nb = [x + 1, y];
const shape = getCell(mx, nb);
if (shape === null) return null;
const connects = "J-7".includes(shape);
return connects ? nb : null;
};
const getNbs = (p: [number, number], matrix: string[][]) => {
const [x, y] = p;
const char = getCell(matrix, p);
const l = connectLeft(matrix, p);
const r = connectRight(matrix, p);
const t = connectTop(matrix, p);
const b = connectBot(matrix, p);
// console.log({ char, p, l, r, t, b });
if (char === "S") return [t, l, r, b];
if (char === "-") return [l, r];
if (char === "|") return [t, b];
if (char === "7") return [l, b];
if (char === "J") return [t, l];
if (char === "L") return [t, r];
if (char === "F") return [b, r];
return [];
};
const solveMatrix = (matrix: string[][]) => {
console.log({ matrix });
// Start pos
const dedupe = new Set();
const start = findStart(matrix);
dedupe.add(start);
const netw = new Map();
let loop = [start];
// let loop = [[1, 3]];
//
for (let x = 0; x < 5; x++) {
console.log({ x, loop });
let next: any[] = [];
loop.map((pos) => {
const nbs = getNbs(pos, matrix)?.filter((nb) => {
if (nb === null) return false;
return !dedupe.has("" + nb[0] + "," + nb[1]);
});
//
console.log({ pos, nbs });
netw.set(pos, nbs);
nbs?.forEach((nb) => {
if (nb) dedupe.add("" + nb[0] + "," + nb[1]);
next.push(nb);
});
});
loop = next;
console.log({ next });
}
console.log(netw);
};
//
const testa = `
.....
.S-7.
.|.|.
.L-J.
.....`;
const testb = `
..F7.
.FJ|.
SJ.L7
|F--J
LJ...`;
const mx = getMatrix(testa);
solveMatrix(mx);
// console.log({ a, mx });