From 1c7aaa8fd58615d38331276adb689c38fff6c186 Mon Sep 17 00:00:00 2001 From: nim-ka Date: Sat, 29 Oct 2022 02:47:26 +0000 Subject: [PATCH] . --- cat.sh | 0 graph.js | 6 +++--- grid.js | 18 +++++++++++------- out.js | 24 ++++++++++++++---------- 4 files changed, 28 insertions(+), 20 deletions(-) mode change 100644 => 100755 cat.sh diff --git a/cat.sh b/cat.sh old mode 100644 new mode 100755 diff --git a/graph.js b/graph.js index d39a6b8..f549b20 100644 --- a/graph.js +++ b/graph.js @@ -62,7 +62,7 @@ class Node { return path } - dijkstra(dests) { + dijkstraTo(dests) { if (!Array.isArray(dests)) { dests = [dests] } @@ -120,13 +120,13 @@ class Node { } }) - if (i++ % 1000 == 0) { + if (++i % 1000 == 0) { console.log(heap.data.length) } } console.timeEnd("search") - console.warn("Node.dijkstra: Could not find a path") + console.warn("Node.dijkstraTo: Could not find a path") } } diff --git a/grid.js b/grid.js index bcb845d..0c5d857 100644 --- a/grid.js +++ b/grid.js @@ -26,12 +26,10 @@ class Grid { return this.mapMut((_, pt) => arr[pt.y][pt.x]) } - fillFromStr(str) { return this.fillFromArr(str.split("\n")) } + fillFromStr(str, sep = "") { return this.fillFromArr(str.split("\n").map((line) => line.split(sep)) } - static fromStr(str) { - let arr = str.split("\n") - return new Grid(arr[0].length, arr.length).fillFromArr(arr) - } + static fromArr(arr) { return new Grid(arr[0].length, arr.length).fillFromArr(arr) } + static fromStr(str, sep = "") { return Grid.fromArr(str.split("\n").map((line) => line.split(sep)) } get(pt) { if (this.contains(pt)) { @@ -99,7 +97,7 @@ class Grid { static BFS_STOP = 1 static BFS_END = 2 - bfs(pt, func, limit = 1000) { + bfs(pt, func, neighbors = "getAdjNeighborsThat", getlimit = 1000) { let visited = [].pt let toVisit = [pt].pt let count = 0 @@ -116,7 +114,7 @@ class Grid { let result = func(this.get(toVisit[i]), toVisit[i], this, visited); if (result == Grid.BFS_CONTINUE) { - newToVisit.pushUniq(...this.getAdjNeighborsThat(toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt))) + newToVisit.pushUniq(...this[neighbors](toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt))) } if (result == Grid.BFS_END) { @@ -178,6 +176,12 @@ class Grid { ] } + toGraph(neighbors = "getAdjNeighbors", cxn = (node, cxnNode) => node.addCxn(cxnNode, cxnNode.val)) { + this.mapMut((e) => new Node(e)) + this.forEach((e, pt) => this[neighbors](pt).forEach((pt) => cxn(e, this.get(pt)))) + return + } + copy() { return new Grid(this.width, this.height).mapMut((_, pt) => this.get(pt).copyDeep()) } toString(sep = "\t", ...pts) { return this.data.map((r, y) => r.map((e, x) => new Point(x, y).isIn(pts) ? "P" : e).join(sep)).join("\n") } print(sep = "\t", ...pts) { console.log(this.toString(sep, pts)) } diff --git a/out.js b/out.js index 90a8962..d1f3869 100644 --- a/out.js +++ b/out.js @@ -262,12 +262,10 @@ class Grid { return this.mapMut((_, pt) => arr[pt.y][pt.x]) } - fillFromStr(str) { return this.fillFromArr(str.split("\n")) } + fillFromStr(str, sep = "") { return this.fillFromArr(str.split("\n").map((line) => line.split(sep)) } - static fromStr(str) { - let arr = str.split("\n") - return new Grid(arr[0].length, arr.length).fillFromArr(arr) - } + static fromArr(arr) { return new Grid(arr[0].length, arr.length).fillFromArr(arr) } + static fromStr(str, sep = "") { return Grid.fromArr(str.split("\n").map((line) => line.split(sep)) } get(pt) { if (this.contains(pt)) { @@ -335,7 +333,7 @@ class Grid { static BFS_STOP = 1 static BFS_END = 2 - bfs(pt, func, limit = 1000) { + bfs(pt, func, neighbors = "getAdjNeighborsThat", getlimit = 1000) { let visited = [].pt let toVisit = [pt].pt let count = 0 @@ -352,7 +350,7 @@ class Grid { let result = func(this.get(toVisit[i]), toVisit[i], this, visited); if (result == Grid.BFS_CONTINUE) { - newToVisit.pushUniq(...this.getAdjNeighborsThat(toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt))) + newToVisit.pushUniq(...this[neighbors](toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt))) } if (result == Grid.BFS_END) { @@ -414,6 +412,12 @@ class Grid { ] } + toGraph(neighbors = "getAdjNeighbors", cxn = (node, cxnNode) => node.addCxn(cxnNode, cxnNode.val)) { + this.mapMut((e) => new Node(e)) + this.forEach((e, pt) => this[neighbors](pt).forEach((pt) => cxn(e, this.get(pt)))) + return + } + copy() { return new Grid(this.width, this.height).mapMut((_, pt) => this.get(pt).copyDeep()) } toString(sep = "\t", ...pts) { return this.data.map((r, y) => r.map((e, x) => new Point(x, y).isIn(pts) ? "P" : e).join(sep)).join("\n") } print(sep = "\t", ...pts) { console.log(this.toString(sep, pts)) } @@ -543,7 +547,7 @@ class Node { return path } - dijkstra(dests) { + dijkstraTo(dests) { if (!Array.isArray(dests)) { dests = [dests] } @@ -601,13 +605,13 @@ class Node { } }) - if (i++ % 1000 == 0) { + if (++i % 1000 == 0) { console.log(heap.data.length) } } console.timeEnd("search") - console.warn("Node.dijkstra: Could not find a path") + console.warn("Node.dijkstraTo: Could not find a path") } }