.
This commit is contained in:
34
2021/11.js
Normal file
34
2021/11.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
function day11(input, part2) {
|
||||||
|
let grid = Grid.fromStr(input).mapMut((e) => ({ energy: +e, flashed: false }))
|
||||||
|
|
||||||
|
let steps = 0
|
||||||
|
let flashes = 0
|
||||||
|
let totalFlashes = 0
|
||||||
|
|
||||||
|
while (part2 ? flashes != grid.width * grid.height : steps < 100) {
|
||||||
|
flashes = 0
|
||||||
|
|
||||||
|
grid.forEach(function flash(e, pt, g) {
|
||||||
|
e.energy++
|
||||||
|
|
||||||
|
if (!e.flashed && e.energy > 9) {
|
||||||
|
e.flashed = true
|
||||||
|
flashes++
|
||||||
|
|
||||||
|
g.getAllNeighborsThat(pt, (e) => !e.flashed).forEach((pt) => flash(g.get(pt), pt, g))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
grid.forEach((e) => {
|
||||||
|
if (e.flashed) {
|
||||||
|
e.energy = 0
|
||||||
|
e.flashed = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
steps++
|
||||||
|
totalFlashes += flashes
|
||||||
|
}
|
||||||
|
|
||||||
|
return part2 ? steps : totalFlashes
|
||||||
|
}
|
||||||
42
2021/12.js
Normal file
42
2021/12.js
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// TODO: not very efficient because i was lazy
|
||||||
|
|
||||||
|
function day12(input, part2) {
|
||||||
|
let cxns = input.split("\n").map((line) => line.split("-"))
|
||||||
|
let dict = {}
|
||||||
|
|
||||||
|
for (let cxn of cxns) {
|
||||||
|
dict[cxn[0]] = [...(dict[cxn[0]] || []), cxn[1]]
|
||||||
|
dict[cxn[1]] = [...(dict[cxn[1]] || []), cxn[0]]
|
||||||
|
}
|
||||||
|
|
||||||
|
let count = 0
|
||||||
|
let paths = [["start"]]
|
||||||
|
|
||||||
|
while (paths.length) {
|
||||||
|
let newPaths = []
|
||||||
|
|
||||||
|
for (let path of paths) {
|
||||||
|
for (let next of dict[path.last]) {
|
||||||
|
if (next == "start") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let newPath = [...path, next]
|
||||||
|
|
||||||
|
if (newPath.count((e, i, a) => e == e.toLowerCase() && a.indexOf(e) != i) > part2) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next == "end") {
|
||||||
|
count++
|
||||||
|
} else {
|
||||||
|
newPaths.push(newPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paths = newPaths
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
}
|
||||||
27
2021/13.js
Normal file
27
2021/13.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
function day13(input, part2) {
|
||||||
|
input = input.split("\n").splitOnElement("")
|
||||||
|
|
||||||
|
let points = input[0].map((e) => new Point(...e.split(",").num())).pt
|
||||||
|
let instructions = input[1].map((e) => e.split(" ").last.split("=")).map((e) => [e[0], +e[1]])
|
||||||
|
|
||||||
|
for (let instruction of instructions) {
|
||||||
|
let pos = instruction[1]
|
||||||
|
let newPoints = [].pt
|
||||||
|
|
||||||
|
for (let point of points) {
|
||||||
|
if (instruction[0] == "x") {
|
||||||
|
newPoints.push(point.x < pos ? point : new Point(2 * pos - point.x, point.y))
|
||||||
|
} else {
|
||||||
|
newPoints.push(point.y < pos ? point : new Point(point.x, 2 * pos - point.y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
points = newPoints.uniq()
|
||||||
|
|
||||||
|
if (!part2) {
|
||||||
|
return points.length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Grid(points.max((e) => e.x).x + 1, points.max((e) => e.y).y + 1).mapMut((_, pt) => pt.isIn(points) ? "#" : " ").toString("")
|
||||||
|
}
|
||||||
9
grid.js
9
grid.js
@@ -80,6 +80,8 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findIndex(func) {
|
findIndex(func) {
|
||||||
|
func = typeof func == "function" ? func : (e) => e == func
|
||||||
|
|
||||||
for (let y = 0; y < this.height; y++) {
|
for (let y = 0; y < this.height; y++) {
|
||||||
for (let x = 0; x < this.width; x++) {
|
for (let x = 0; x < this.width; x++) {
|
||||||
let pt = new Point(x, y)
|
let pt = new Point(x, y)
|
||||||
@@ -97,8 +99,11 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findAllIndices(func) {
|
findAllIndices(func) {
|
||||||
|
func = typeof func == "function" ? func : (e) => e == func
|
||||||
|
|
||||||
let points = [].pt
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,6 +111,10 @@ Grid = class Grid {
|
|||||||
return this.findAllIndices(func).map((pt) => this.get(pt))
|
return this.findAllIndices(func).map((pt) => this.get(pt))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count(func) {
|
||||||
|
return this.findAllIndices(func).length
|
||||||
|
}
|
||||||
|
|
||||||
indexOf(val) {
|
indexOf(val) {
|
||||||
return this.findIndex((e) => e == val)
|
return this.findIndex((e) => e == val)
|
||||||
}
|
}
|
||||||
|
|||||||
21
out.js
21
out.js
@@ -199,12 +199,12 @@ Pt = Point = class Point {
|
|||||||
dl() { return this.downleft() }
|
dl() { return this.downleft() }
|
||||||
dr() { return this.downright() }
|
dr() { return this.downright() }
|
||||||
|
|
||||||
getUnfilteredAdjNeighbors() { return [this.u(), this.l(), this.r(), this.d()] }
|
getUnfilteredAdjNeighbors() { return new PointArray(this.u(), this.l(), this.r(), this.d()) }
|
||||||
getUnfilteredDiagNeighbors() { return [this.ul(), this.ur(), this.dl(), this.dr()] }
|
getUnfilteredDiagNeighbors() { return new PointArray(this.ul(), this.ur(), this.dl(), this.dr()) }
|
||||||
getUnfilteredAllNeighbors() { return [this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()] }
|
getUnfilteredAllNeighbors() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()) }
|
||||||
getUnfilteredAdjNeighborsIncSelf() { return [this.u(), this.l(), this, this.r(), this.d()] }
|
getUnfilteredAdjNeighborsIncSelf() { return new PointArray(this.u(), this.l(), this, this.r(), this.d()) }
|
||||||
getUnfilteredDiagNeighborsIncSelf() { return [this.ul(), this.ur(), this, this.dl(), this.dr()] }
|
getUnfilteredDiagNeighborsIncSelf() { return new PointArray(this.ul(), this.ur(), this, this.dl(), this.dr()) }
|
||||||
getUnfilteredAllNeighborsIncSelf() { return [this.ul(), this.u(), this.ur(), this.l(), this, this.r(), this.dl(), this.d(), this.dr()] }
|
getUnfilteredAllNeighborsIncSelf() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this, this.r(), this.dl(), this.d(), this.dr()) }
|
||||||
|
|
||||||
add(pt) { return new Point(this.x + pt.x, this.y + pt.y) }
|
add(pt) { return new Point(this.x + pt.x, this.y + pt.y) }
|
||||||
addMut(pt) {
|
addMut(pt) {
|
||||||
@@ -321,6 +321,8 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findIndex(func) {
|
findIndex(func) {
|
||||||
|
func = typeof func == "function" ? func : (e) => e == func
|
||||||
|
|
||||||
for (let y = 0; y < this.height; y++) {
|
for (let y = 0; y < this.height; y++) {
|
||||||
for (let x = 0; x < this.width; x++) {
|
for (let x = 0; x < this.width; x++) {
|
||||||
let pt = new Point(x, y)
|
let pt = new Point(x, y)
|
||||||
@@ -338,8 +340,11 @@ Grid = class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findAllIndices(func) {
|
findAllIndices(func) {
|
||||||
|
func = typeof func == "function" ? func : (e) => e == func
|
||||||
|
|
||||||
let points = [].pt
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,6 +352,10 @@ Grid = class Grid {
|
|||||||
return this.findAllIndices(func).map((pt) => this.get(pt))
|
return this.findAllIndices(func).map((pt) => this.get(pt))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count(func) {
|
||||||
|
return this.findAllIndices(func).length
|
||||||
|
}
|
||||||
|
|
||||||
indexOf(val) {
|
indexOf(val) {
|
||||||
return this.findIndex((e) => e == val)
|
return this.findIndex((e) => e == val)
|
||||||
}
|
}
|
||||||
|
|||||||
12
pt.js
12
pt.js
@@ -27,12 +27,12 @@ Pt = Point = class Point {
|
|||||||
dl() { return this.downleft() }
|
dl() { return this.downleft() }
|
||||||
dr() { return this.downright() }
|
dr() { return this.downright() }
|
||||||
|
|
||||||
getUnfilteredAdjNeighbors() { return [this.u(), this.l(), this.r(), this.d()] }
|
getUnfilteredAdjNeighbors() { return new PointArray(this.u(), this.l(), this.r(), this.d()) }
|
||||||
getUnfilteredDiagNeighbors() { return [this.ul(), this.ur(), this.dl(), this.dr()] }
|
getUnfilteredDiagNeighbors() { return new PointArray(this.ul(), this.ur(), this.dl(), this.dr()) }
|
||||||
getUnfilteredAllNeighbors() { return [this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()] }
|
getUnfilteredAllNeighbors() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()) }
|
||||||
getUnfilteredAdjNeighborsIncSelf() { return [this.u(), this.l(), this, this.r(), this.d()] }
|
getUnfilteredAdjNeighborsIncSelf() { return new PointArray(this.u(), this.l(), this, this.r(), this.d()) }
|
||||||
getUnfilteredDiagNeighborsIncSelf() { return [this.ul(), this.ur(), this, this.dl(), this.dr()] }
|
getUnfilteredDiagNeighborsIncSelf() { return new PointArray(this.ul(), this.ur(), this, this.dl(), this.dr()) }
|
||||||
getUnfilteredAllNeighborsIncSelf() { return [this.ul(), this.u(), this.ur(), this.l(), this, this.r(), this.dl(), this.d(), this.dr()] }
|
getUnfilteredAllNeighborsIncSelf() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this, this.r(), this.dl(), this.d(), this.dr()) }
|
||||||
|
|
||||||
add(pt) { return new Point(this.x + pt.x, this.y + pt.y) }
|
add(pt) { return new Point(this.x + pt.x, this.y + pt.y) }
|
||||||
addMut(pt) {
|
addMut(pt) {
|
||||||
|
|||||||
Reference in New Issue
Block a user