This commit is contained in:
nim-ka
2022-11-08 22:30:37 +00:00
parent b5e7f07541
commit f8e9088e67
13 changed files with 616 additions and 341 deletions

3
2021/1.js Normal file
View File

@@ -0,0 +1,3 @@
function day1(input, part2) {
return input.split`\n`.num().count((e, i, a, k = a[i - (part2 ? 3 : 1)]) => k && e > k)
}

15
2021/15.js Normal file
View File

@@ -0,0 +1,15 @@
function day15(input, part2) {
let g = Grid.fromStr(input).num()
if (part2) {
g = new Grid(g.width * 5, g.height * 5).mapMut((e, pt) => ((g.get(new Pt(pt.x % g.width, pt.y % g.height)) + (pt.x / g.width | 0) + (pt.y / g.height | 0) - 1) % 9) + 1)
}
g.graphify()
let start = g.get(new Pt(0, 0))
let end = g.get(new Pt(g.width - 1, g.height - 1))
start.dijkstraTo(end)
return end.searchData.dist
}

15
2021/2.js Normal file
View File

@@ -0,0 +1,15 @@
function day2(input, part2) {
let x = 0
let y = 0
let a = 0
input.split`\n`.forEach((e) => {
if (!part2) {
eval(e.replace("forward", "x +=").replace("up", "y -=").replace("down", "y +="))
} else {
eval(e.replace(/forward (.+)/, "x += $1; y += a * $1").replace("up", "a -=").replace("down", "a +="))
}
})
return x * y
}

View File

@@ -1,4 +1,4 @@
class BinHeap {
BinHeap = class BinHeap {
constructor(cond = (p, c) => p < c) {
this.cond = cond
this.data = []

2
cat.sh
View File

@@ -1,3 +1,3 @@
#!/bin/bash
cat dll.js pt.js grid.js binheap.js graph.js proto.js utils.js test.js > out.js
cat dll.js pt.js grid.js binheap.js graph.js proto.js utils.js repl.js > out.js

4
dll.js
View File

@@ -2,7 +2,7 @@
// - copy method
// - remove range method
class DLLNode {
DLLNode = class DLLNode {
constructor(val, skip = this, prev = this, next = this) {
this.val = val
this.skip = skip
@@ -29,7 +29,7 @@ class DLLNode {
}
}
class DLL {
DLL = class DLL {
constructor(a = []) {
this.h = new DLLNode()
this.length = 0;

View File

@@ -1,11 +1,11 @@
class Cxn {
Cxn = class Cxn {
constructor(dest, weight = 1) {
this.dest = dest
this.weight = weight
}
}
class SearchData {
SearchData = class SearchData {
constructor(id, dist = Infinity, last = undefined, custom = {}) {
this.id = id
this.dist = dist
@@ -37,7 +37,7 @@ class SearchData {
}
}
class Node {
Node = class Node {
static GLOBAL_ID = 0
constructor(val) {

View File

@@ -1,4 +1,4 @@
class Grid {
Grid = class Grid {
constructor(w, h, fill = 0) {
this.width = w
this.height = h

461
out.js
View File

@@ -2,7 +2,7 @@
// - copy method
// - remove range method
class DLLNode {
DLLNode = class DLLNode {
constructor(val, skip = this, prev = this, next = this) {
this.val = val
this.skip = skip
@@ -29,7 +29,7 @@ class DLLNode {
}
}
class DLL {
DLL = class DLL {
constructor(a = []) {
this.h = new DLLNode()
this.length = 0;
@@ -170,7 +170,7 @@ class DLL {
toString() { return this.toArray().toString() }
}
class Point {
Pt = Point = class Point {
constructor(x, y) {
this.x = x
this.y = y
@@ -232,14 +232,12 @@ class Point {
toString() { return this.x + "," + this.y }
}
Pt = Point
function P(...args) {
P = function P(...args) {
return new Point(...args)
}
class Grid {
Grid = class Grid {
constructor(w, h, fill = 0) {
this.width = w
this.height = h
@@ -428,7 +426,7 @@ class Grid {
print(sep = "\t", ...pts) { console.log(this.toString(sep, pts)) }
}
class BinHeap {
BinHeap = class BinHeap {
constructor(cond = (p, c) => p < c) {
this.cond = cond
this.data = []
@@ -488,14 +486,14 @@ class BinHeap {
}
}
class Cxn {
Cxn = class Cxn {
constructor(dest, weight = 1) {
this.dest = dest
this.weight = weight
}
}
class SearchData {
SearchData = class SearchData {
constructor(id, dist = Infinity, last = undefined, custom = {}) {
this.id = id
this.dist = dist
@@ -527,7 +525,7 @@ class SearchData {
}
}
class Node {
Node = class Node {
static GLOBAL_ID = 0
constructor(val) {
@@ -620,117 +618,7 @@ class Node {
}
}
let warned = false
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")
}
}
}
})
Object.defineProperties(Array.prototype, {
dll: {
value: function() {
return new DLL(this)
}
},
copy: {
value: function() {
return this.slice()
}
},
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
}
},
sum: {
value: function(val) {
return this.reduce((a, b) => a + b, val)
}
},
flatDeep: {
value: function() {
return this.flat(Infinity)
}
},
transpose: {
value: function() {
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
}
},
findLast: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return this[i]
}
}
}
},
findLastIndex: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return i
}
}
return -1
}
},
interleave: {
value: function(that) {
return [this, that].transpose().flat()
}
},
rotate: {
value: function(n) {
let k = (this.length + n) % this.length
return [...this.slice(k), ...this.slice(0, k)]
}
},
sub: {
value: function(that) {
return this.filter(e => !that.includes(e))
}
},
int: {
value: function(that) {
return this.filter(e => that.includes(e))
}
},
uniq: {
value: function() {
return this.filter((e, i) => this.indexOf(e) == i)
}
},
pushUniq: {
value: function(...vals) {
if (!warned) {
console.warn("You should probably use a Set")
warned = true
}
return this.push(...vals.uniq().sub(this))
}
}
})
class PointArray extends Array {
PtArray = PointArray = class PointArray extends Array {
static convert(arr) {
if (!(arr instanceof PointArray)) {
arr.__proto__ = PointArray.prototype
@@ -740,57 +628,287 @@ class PointArray extends Array {
}
}
PtArray = PointArray
let warned = false
Object.defineProperty(Array.prototype, "pt", {
get: function() {
return PointArray.convert(this)
}
})
load = function load() {
Object.defineProperties(Object.prototype, {
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
},
configurable: true
},
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")
}
},
configurable: true
}
})
Object.defineProperties(PointArray.prototype, {
sort: {
value: function(func = (a, b) => a.readingOrderCompare(b)) {
return Array.prototype.sort.apply(this, func)
}
},
includes: {
value: function(pt) {
return pt.isIn(this)
}
},
indexOf: {
value: function(pt) {
return pt.indexIn(this)
}
},
lastIndexOf: {
value: function(pt) {
return pt.lastIndexIn(this)
}
},
sub: {
value: function(that) {
return this.filter(e => !that.pt.includes(e))
}
},
int: {
value: function(that) {
return this.filter(e => that.pt.includes(e))
}
},
uniq: {
value: function() {
return this.filter((e, i) => this.pt.indexOf(e) == i)
}
},
pushUniq: {
value: function(...vals) {
return this.push(...vals.pt.uniq().pt.sub(this))
}
}
})
Object.defineProperties(Array.prototype, {
pt: {
get: function() {
return PointArray.convert(this)
},
configurable: true
},
last: {
get: function() {
return this[this.length - 1]
},
set: function(val) {
this[this.length - 1] = val
},
configurable: true
},
dll: {
value: function() {
return new DLL(this)
},
configurable: true
},
copy: {
value: function() {
return this.slice()
},
configurable: true
},
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
},
configurable: true
},
sum: {
value: function(val) {
return this.reduce((a, b) => a + b, val)
},
configurable: true
},
flatDeep: {
value: function() {
return this.flat(Infinity)
},
configurable: true
},
transpose: {
value: function() {
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
},
configurable: true
},
findLast: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return this[i]
}
}
},
configurable: true
},
findLastIndex: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return i
}
}
return -1
},
configurable: true
},
interleave: {
value: function(that) {
return [this, that].transpose().flat()
},
configurable: true
},
rotate: {
value: function(n) {
let k = (this.length + n) % this.length
return [...this.slice(k), ...this.slice(0, k)]
},
configurable: true
},
sub: {
value: function(that) {
return this.filter(e => !that.includes(e))
},
configurable: true
},
int: {
value: function(that) {
return this.filter(e => that.includes(e))
},
configurable: true
},
uniq: {
value: function() {
return this.filter((e, i) => this.indexOf(e) == i)
},
configurable: true
},
pushUniq: {
value: function(...vals) {
if (!warned) {
console.warn("You should probably use a Set")
warned = true
}
return this.push(...vals.uniq().sub(this))
},
configurable: true
},
count: {
value: function(fn) {
return this.filter(typeof fn == "function" ? fn : (e) => e == fn).length
},
configurable: true
},
minIndex: {
value: function(fn = (e) => e, tiebreak) {
let min = Infinity
let idx
for (let i = 0; i < this.length; i++) {
let val = fn(this[i])
if (min > val ||
(min == val && tiebreak && tiebreak(min, val, idx, i) > 0)) {
min = val
idx = i
}
}
return idx
},
configurable: true
},
min: {
value: function(fn, tiebreak) {
return this[this.minIndex(fn, tiebreak)]
},
configurable: true
},
maxIndex: {
value: function(fn = (e) => e, tiebreak) {
let max = -Infinity
let idx
for (let i = 0; i < this.length; i++) {
let val = fn(this[i])
if (max < val ||
(max == val && tiebreak && tiebreak(max, val, idx, i) > 0)) {
max = val
idx = i
}
}
return idx
},
configurable: true
},
max: {
value: function(fn, tiebreak) {
return this[this.maxIndex(fn, tiebreak)]
},
configurable: true
},
mean: {
value: function() {
return this.sum() / this.length
},
configurable: true
},
freqs: {
value: function() {
return this.uniq().map((e) => [e, this.count(e)])
},
configurable: true
},
mode: {
value: function(tiebreak) {
return this.freqs().max((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
},
configurable: true
},
antimode: {
value: function(tiebreak) {
return this.freqs().min((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
},
configurable: true
}
})
Object.defineProperties(PointArray.prototype, {
sort: {
value: function(func = (a, b) => a.readingOrderCompare(b)) {
return Array.prototype.sort.apply(this, func)
},
configurable: true
},
includes: {
value: function(pt) {
return pt.isIn(this)
},
configurable: true
},
indexOf: {
value: function(pt) {
return pt.indexIn(this)
},
configurable: true
},
lastIndexOf: {
value: function(pt) {
return pt.lastIndexIn(this)
},
configurable: true
},
sub: {
value: function(that) {
return this.filter(e => !that.pt.includes(e))
},
configurable: true
},
int: {
value: function(that) {
return this.filter(e => that.pt.includes(e))
},
configurable: true
},
uniq: {
value: function() {
return this.filter((e, i) => this.pt.indexOf(e) == i)
},
configurable: true
},
pushUniq: {
value: function(...vals) {
return this.push(...vals.pt.uniq().pt.sub(this))
},
configurable: true
},
count: {
value: function(fn) {
return this.filter(typeof fn == "function" ? fn : (e) => e.equals(fn)).length
},
configurable: true
}
})
}
load()
utils = {
fetch: (url) => fetch(url).then(e => e.text()),
fetchEval: (url) => utils.fetch(url).then(e => eval(e)),
@@ -798,3 +916,6 @@ utils = {
createGridArray: (w, h, fill = undefined) => Array(h).fill().map(() => Array(w).fill(fill))
}
if (typeof window == "undefined") {
debugger
}

438
proto.js
View File

@@ -1,114 +1,4 @@
let warned = false
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")
}
}
}
})
Object.defineProperties(Array.prototype, {
dll: {
value: function() {
return new DLL(this)
}
},
copy: {
value: function() {
return this.slice()
}
},
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
}
},
sum: {
value: function(val) {
return this.reduce((a, b) => a + b, val)
}
},
flatDeep: {
value: function() {
return this.flat(Infinity)
}
},
transpose: {
value: function() {
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
}
},
findLast: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return this[i]
}
}
}
},
findLastIndex: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return i
}
}
return -1
}
},
interleave: {
value: function(that) {
return [this, that].transpose().flat()
}
},
rotate: {
value: function(n) {
let k = (this.length + n) % this.length
return [...this.slice(k), ...this.slice(0, k)]
}
},
sub: {
value: function(that) {
return this.filter(e => !that.includes(e))
}
},
int: {
value: function(that) {
return this.filter(e => that.includes(e))
}
},
uniq: {
value: function() {
return this.filter((e, i) => this.indexOf(e) == i)
}
},
pushUniq: {
value: function(...vals) {
if (!warned) {
console.warn("You should probably use a Set")
warned = true
}
return this.push(...vals.uniq().sub(this))
}
}
})
class PointArray extends Array {
PtArray = PointArray = class PointArray extends Array {
static convert(arr) {
if (!(arr instanceof PointArray)) {
arr.__proto__ = PointArray.prototype
@@ -118,54 +8,284 @@ class PointArray extends Array {
}
}
PtArray = PointArray
let warned = false
Object.defineProperty(Array.prototype, "pt", {
get: function() {
return PointArray.convert(this)
}
})
load = function load() {
Object.defineProperties(Object.prototype, {
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
},
configurable: true
},
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")
}
},
configurable: true
}
})
Object.defineProperties(PointArray.prototype, {
sort: {
value: function(func = (a, b) => a.readingOrderCompare(b)) {
return Array.prototype.sort.apply(this, func)
}
},
includes: {
value: function(pt) {
return pt.isIn(this)
}
},
indexOf: {
value: function(pt) {
return pt.indexIn(this)
}
},
lastIndexOf: {
value: function(pt) {
return pt.lastIndexIn(this)
}
},
sub: {
value: function(that) {
return this.filter(e => !that.pt.includes(e))
}
},
int: {
value: function(that) {
return this.filter(e => that.pt.includes(e))
}
},
uniq: {
value: function() {
return this.filter((e, i) => this.pt.indexOf(e) == i)
}
},
pushUniq: {
value: function(...vals) {
return this.push(...vals.pt.uniq().pt.sub(this))
}
}
})
Object.defineProperties(Array.prototype, {
pt: {
get: function() {
return PointArray.convert(this)
},
configurable: true
},
last: {
get: function() {
return this[this.length - 1]
},
set: function(val) {
this[this.length - 1] = val
},
configurable: true
},
dll: {
value: function() {
return new DLL(this)
},
configurable: true
},
copy: {
value: function() {
return this.slice()
},
configurable: true
},
copyDeep: {
value: function() {
return JSON.parse(JSON.stringify(this))
},
configurable: true
},
sum: {
value: function(val) {
return this.reduce((a, b) => a + b, val)
},
configurable: true
},
flatDeep: {
value: function() {
return this.flat(Infinity)
},
configurable: true
},
transpose: {
value: function() {
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
},
configurable: true
},
findLast: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return this[i]
}
}
},
configurable: true
},
findLastIndex: {
value: function(func) {
for (let i = this.length - 1; i >= 0; i--) {
if (func(this[i], i, this)) {
return i
}
}
return -1
},
configurable: true
},
interleave: {
value: function(that) {
return [this, that].transpose().flat()
},
configurable: true
},
rotate: {
value: function(n) {
let k = (this.length + n) % this.length
return [...this.slice(k), ...this.slice(0, k)]
},
configurable: true
},
sub: {
value: function(that) {
return this.filter(e => !that.includes(e))
},
configurable: true
},
int: {
value: function(that) {
return this.filter(e => that.includes(e))
},
configurable: true
},
uniq: {
value: function() {
return this.filter((e, i) => this.indexOf(e) == i)
},
configurable: true
},
pushUniq: {
value: function(...vals) {
if (!warned) {
console.warn("You should probably use a Set")
warned = true
}
return this.push(...vals.uniq().sub(this))
},
configurable: true
},
count: {
value: function(fn) {
return this.filter(typeof fn == "function" ? fn : (e) => e == fn).length
},
configurable: true
},
minIndex: {
value: function(fn = (e) => e, tiebreak) {
let min = Infinity
let idx
for (let i = 0; i < this.length; i++) {
let val = fn(this[i])
if (min > val ||
(min == val && tiebreak && tiebreak(min, val, idx, i) > 0)) {
min = val
idx = i
}
}
return idx
},
configurable: true
},
min: {
value: function(fn, tiebreak) {
return this[this.minIndex(fn, tiebreak)]
},
configurable: true
},
maxIndex: {
value: function(fn = (e) => e, tiebreak) {
let max = -Infinity
let idx
for (let i = 0; i < this.length; i++) {
let val = fn(this[i])
if (max < val ||
(max == val && tiebreak && tiebreak(max, val, idx, i) > 0)) {
max = val
idx = i
}
}
return idx
},
configurable: true
},
max: {
value: function(fn, tiebreak) {
return this[this.maxIndex(fn, tiebreak)]
},
configurable: true
},
mean: {
value: function() {
return this.sum() / this.length
},
configurable: true
},
freqs: {
value: function() {
return this.uniq().map((e) => [e, this.count(e)])
},
configurable: true
},
mode: {
value: function(tiebreak) {
return this.freqs().max((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
},
configurable: true
},
antimode: {
value: function(tiebreak) {
return this.freqs().min((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
},
configurable: true
}
})
Object.defineProperties(PointArray.prototype, {
sort: {
value: function(func = (a, b) => a.readingOrderCompare(b)) {
return Array.prototype.sort.apply(this, func)
},
configurable: true
},
includes: {
value: function(pt) {
return pt.isIn(this)
},
configurable: true
},
indexOf: {
value: function(pt) {
return pt.indexIn(this)
},
configurable: true
},
lastIndexOf: {
value: function(pt) {
return pt.lastIndexIn(this)
},
configurable: true
},
sub: {
value: function(that) {
return this.filter(e => !that.pt.includes(e))
},
configurable: true
},
int: {
value: function(that) {
return this.filter(e => that.pt.includes(e))
},
configurable: true
},
uniq: {
value: function() {
return this.filter((e, i) => this.pt.indexOf(e) == i)
},
configurable: true
},
pushUniq: {
value: function(...vals) {
return this.push(...vals.pt.uniq().pt.sub(this))
},
configurable: true
},
count: {
value: function(fn) {
return this.filter(typeof fn == "function" ? fn : (e) => e.equals(fn)).length
},
configurable: true
}
})
}
load()

6
pt.js
View File

@@ -1,4 +1,4 @@
class Point {
Pt = Point = class Point {
constructor(x, y) {
this.x = x
this.y = y
@@ -60,9 +60,7 @@ class Point {
toString() { return this.x + "," + this.y }
}
Pt = Point
function P(...args) {
P = function P(...args) {
return new Point(...args)
}

3
repl.js Normal file
View File

@@ -0,0 +1,3 @@
if (typeof window == "undefined") {
debugger
}

View File