.
This commit is contained in:
30
2021/10.js
Normal file
30
2021/10.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
const opens = ["(", "[", "{", "<"]
|
||||||
|
const closes = [")", "]", "}", ">"]
|
||||||
|
const scores1 = { ")": 3, "]": 57, "}": 1197, ">": 25137 }
|
||||||
|
const scores2 = { ")": 1, "]": 2, "}": 3, ">": 4 }
|
||||||
|
|
||||||
|
function parse(str) {
|
||||||
|
let stack = []
|
||||||
|
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
let char = str[i]
|
||||||
|
|
||||||
|
if (opens.includes(char)) {
|
||||||
|
stack.push(closes[opens.indexOf(char)])
|
||||||
|
} else if (char != stack.pop()) {
|
||||||
|
return scores1[char]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stack.reverse()
|
||||||
|
}
|
||||||
|
|
||||||
|
function day10(input, part2) {
|
||||||
|
let results = input.split("\n").map(parse)
|
||||||
|
|
||||||
|
if (!part2) {
|
||||||
|
return results.filter((e) => !Array.isArray(e)).sum()
|
||||||
|
} else {
|
||||||
|
return results.filter((e) => Array.isArray(e)).map((e) => e.reduce((a, b) => a * 5 + scores2[b], 0)).medianNumeric()
|
||||||
|
}
|
||||||
|
}
|
||||||
15
2021/9.js
Normal file
15
2021/9.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
function day9(input, part2) {
|
||||||
|
let grid = Grid.fromStr(input).num()
|
||||||
|
|
||||||
|
let lows = grid.findAllIndices((e, pt, g) => g.getAdjNeighbors(pt).every((nb) => e < g.get(nb)))
|
||||||
|
|
||||||
|
if (!part2) {
|
||||||
|
return lows.map((e) => grid.get(e) + 1).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
let sizes = lows.map((low) => grid.bfs(low, (e, pt, g) => {
|
||||||
|
return e == 9 || g.get(pt.path.last) < e ? Grid.BFS_STOP : Grid.BFS_CONTINUE
|
||||||
|
}).filter((e) => e.result == Grid.BFS_CONTINUE).length).sort((a, b) => b - a)
|
||||||
|
|
||||||
|
return sizes[0] * sizes[1] * sizes[2]
|
||||||
|
}
|
||||||
16
grid.js
16
grid.js
@@ -97,7 +97,7 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findAllIndices(func) {
|
findAllIndices(func) {
|
||||||
let points = []
|
let points = [].pt
|
||||||
this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0)
|
this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0)
|
||||||
return points
|
return points
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ Grid = class Grid {
|
|||||||
static BFS_STOP = 1
|
static BFS_STOP = 1
|
||||||
static BFS_END = 2
|
static BFS_END = 2
|
||||||
|
|
||||||
bfs(pt, func, neighbors = "getAdjNeighborsThat", getlimit = 1000) {
|
bfs(pt, func, neighbors = "getAdjNeighborsThat", limit = 1000) {
|
||||||
let visited = [].pt
|
let visited = [].pt
|
||||||
let toVisit = [pt].pt
|
let toVisit = [pt].pt
|
||||||
let count = 0
|
let count = 0
|
||||||
@@ -144,14 +144,16 @@ Grid = class Grid {
|
|||||||
toVisit.sort()
|
toVisit.sort()
|
||||||
|
|
||||||
for (let i = 0; i < toVisit.length; i++) {
|
for (let i = 0; i < toVisit.length; i++) {
|
||||||
let result = func(this.get(toVisit[i]), toVisit[i], this, visited);
|
let v = toVisit[i]
|
||||||
|
|
||||||
if (result == Grid.BFS_CONTINUE) {
|
v.result = func(this.get(v), v, this, visited)
|
||||||
newToVisit.pushUniq(...this[neighbors](toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt)))
|
|
||||||
|
if (v.result == Grid.BFS_CONTINUE) {
|
||||||
|
newToVisit.pushUniq(...this[neighbors](v, (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...v.path, pt], pt)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == Grid.BFS_END) {
|
if (v.result == Grid.BFS_END) {
|
||||||
end = toVisit[i]
|
end = v
|
||||||
break out
|
break out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
out.js
47
out.js
@@ -338,7 +338,7 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findAllIndices(func) {
|
findAllIndices(func) {
|
||||||
let points = []
|
let points = [].pt
|
||||||
this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0)
|
this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0)
|
||||||
return points
|
return points
|
||||||
}
|
}
|
||||||
@@ -371,7 +371,7 @@ Grid = class Grid {
|
|||||||
static BFS_STOP = 1
|
static BFS_STOP = 1
|
||||||
static BFS_END = 2
|
static BFS_END = 2
|
||||||
|
|
||||||
bfs(pt, func, neighbors = "getAdjNeighborsThat", getlimit = 1000) {
|
bfs(pt, func, neighbors = "getAdjNeighborsThat", limit = 1000) {
|
||||||
let visited = [].pt
|
let visited = [].pt
|
||||||
let toVisit = [pt].pt
|
let toVisit = [pt].pt
|
||||||
let count = 0
|
let count = 0
|
||||||
@@ -385,14 +385,16 @@ Grid = class Grid {
|
|||||||
toVisit.sort()
|
toVisit.sort()
|
||||||
|
|
||||||
for (let i = 0; i < toVisit.length; i++) {
|
for (let i = 0; i < toVisit.length; i++) {
|
||||||
let result = func(this.get(toVisit[i]), toVisit[i], this, visited);
|
let v = toVisit[i]
|
||||||
|
|
||||||
if (result == Grid.BFS_CONTINUE) {
|
v.result = func(this.get(v), v, this, visited)
|
||||||
newToVisit.pushUniq(...this[neighbors](toVisit[i], (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...toVisit[i].path, pt], pt)))
|
|
||||||
|
if (v.result == Grid.BFS_CONTINUE) {
|
||||||
|
newToVisit.pushUniq(...this[neighbors](v, (pt) => !pt.isIn(visited)).map((pt) => (pt.path = [...v.path, pt], pt)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == Grid.BFS_END) {
|
if (v.result == Grid.BFS_END) {
|
||||||
end = toVisit[i]
|
end = v
|
||||||
break out
|
break out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -661,6 +663,14 @@ PtArray = PointArray = class PointArray extends Array {
|
|||||||
|
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static revert(arr) {
|
||||||
|
if (arr.__proto__ != Array.prototype) {
|
||||||
|
arr.__proto__ = Array.prototype
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let warned = false
|
let warned = false
|
||||||
@@ -891,6 +901,17 @@ load = function load() {
|
|||||||
},
|
},
|
||||||
configurable: true
|
configurable: true
|
||||||
},
|
},
|
||||||
|
medianNumeric: {
|
||||||
|
value: function() {
|
||||||
|
let sorted = this.copy().sort((a, b) => a - b)
|
||||||
|
|
||||||
|
if (sorted.length % 2) {
|
||||||
|
return sorted[(sorted.length - 1) / 2]
|
||||||
|
} else {
|
||||||
|
return (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
freqs: {
|
freqs: {
|
||||||
value: function() {
|
value: function() {
|
||||||
return this.uniq().map((e) => [e, this.count(e)])
|
return this.uniq().map((e) => [e, this.count(e)])
|
||||||
@@ -912,6 +933,18 @@ load = function load() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Object.defineProperties(PointArray.prototype, {
|
Object.defineProperties(PointArray.prototype, {
|
||||||
|
arr: {
|
||||||
|
get: function() {
|
||||||
|
return PointArray.revert(this)
|
||||||
|
},
|
||||||
|
configurable: true
|
||||||
|
},
|
||||||
|
map: {
|
||||||
|
value: function(...args) {
|
||||||
|
return this.arr.map(...args)
|
||||||
|
},
|
||||||
|
configurable: true
|
||||||
|
},
|
||||||
splitOnElement: {
|
splitOnElement: {
|
||||||
value: function(sep) {
|
value: function(sep) {
|
||||||
let arr = [[]]
|
let arr = [[]]
|
||||||
|
|||||||
31
proto.js
31
proto.js
@@ -6,6 +6,14 @@ PtArray = PointArray = class PointArray extends Array {
|
|||||||
|
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static revert(arr) {
|
||||||
|
if (arr.__proto__ != Array.prototype) {
|
||||||
|
arr.__proto__ = Array.prototype
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let warned = false
|
let warned = false
|
||||||
@@ -236,6 +244,17 @@ load = function load() {
|
|||||||
},
|
},
|
||||||
configurable: true
|
configurable: true
|
||||||
},
|
},
|
||||||
|
medianNumeric: {
|
||||||
|
value: function() {
|
||||||
|
let sorted = this.copy().sort((a, b) => a - b)
|
||||||
|
|
||||||
|
if (sorted.length % 2) {
|
||||||
|
return sorted[(sorted.length - 1) / 2]
|
||||||
|
} else {
|
||||||
|
return (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
freqs: {
|
freqs: {
|
||||||
value: function() {
|
value: function() {
|
||||||
return this.uniq().map((e) => [e, this.count(e)])
|
return this.uniq().map((e) => [e, this.count(e)])
|
||||||
@@ -257,6 +276,18 @@ load = function load() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Object.defineProperties(PointArray.prototype, {
|
Object.defineProperties(PointArray.prototype, {
|
||||||
|
arr: {
|
||||||
|
get: function() {
|
||||||
|
return PointArray.revert(this)
|
||||||
|
},
|
||||||
|
configurable: true
|
||||||
|
},
|
||||||
|
map: {
|
||||||
|
value: function(...args) {
|
||||||
|
return this.arr.map(...args)
|
||||||
|
},
|
||||||
|
configurable: true
|
||||||
|
},
|
||||||
splitOnElement: {
|
splitOnElement: {
|
||||||
value: function(sep) {
|
value: function(sep) {
|
||||||
let arr = [[]]
|
let arr = [[]]
|
||||||
|
|||||||
Reference in New Issue
Block a user