This commit is contained in:
nim-ka
2022-10-29 02:54:53 +00:00
parent bc0bbdccd2
commit 2aa7b15848
2 changed files with 23 additions and 10 deletions

View File

@@ -29,7 +29,7 @@ class Grid {
fillFromStr(str, sep = "") { return this.fillFromArr(str.split("\n").map((line) => line.split(sep))) }
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)) }
static fromStr(str, sep = "") { return Grid.fromArr(str.split("\n").map((line) => line.split(sep))) }
get(pt) {
if (this.contains(pt)) {

25
out.js
View File

@@ -262,10 +262,10 @@ class Grid {
return this.mapMut((_, pt) => arr[pt.y][pt.x])
}
fillFromStr(str, sep = "") { return this.fillFromArr(str.split("\n").map((line) => line.split(sep)) }
fillFromStr(str, sep = "") { return this.fillFromArr(str.split("\n").map((line) => line.split(sep))) }
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)) }
static fromStr(str, sep = "") { return Grid.fromArr(str.split("\n").map((line) => line.split(sep))) }
get(pt) {
if (this.contains(pt)) {
@@ -412,7 +412,7 @@ class Grid {
]
}
toGraph(neighbors = "getAdjNeighbors", cxn = (node, cxnNode) => node.addCxn(cxnNode, cxnNode.val)) {
graphify(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
@@ -617,9 +617,22 @@ class Node {
let warned = false
Object.defineProperty(Object.prototype, "copyDeep", {
value: function() {
return JSON.parse(JSON.stringify(this))
Object.defineProperties(Object.prototype, {
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
}
},
num: {
value: function() {
if (this.map) {
return this.map((e) => +e)
} else if (this.mapMut) {
return this.mapMut((e) => +e)
} else {
console.error("Object.prototype.num: No suitable map method found")
}
}
}
})