diff --git a/2021/11.js b/2021/11.js new file mode 100644 index 0000000..dbbf173 --- /dev/null +++ b/2021/11.js @@ -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 +} diff --git a/2021/12.js b/2021/12.js new file mode 100644 index 0000000..6b17b7a --- /dev/null +++ b/2021/12.js @@ -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 +} diff --git a/2021/13.js b/2021/13.js new file mode 100644 index 0000000..56aaf3e --- /dev/null +++ b/2021/13.js @@ -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("") +} diff --git a/grid.js b/grid.js index 6ad3314..ef2ac8c 100644 --- a/grid.js +++ b/grid.js @@ -80,6 +80,8 @@ Grid = class Grid { } findIndex(func) { + func = typeof func == "function" ? func : (e) => e == func + for (let y = 0; y < this.height; y++) { for (let x = 0; x < this.width; x++) { let pt = new Point(x, y) @@ -97,8 +99,11 @@ Grid = class Grid { } findAllIndices(func) { + func = typeof func == "function" ? func : (e) => e == func + let points = [].pt this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0) + return points } @@ -106,6 +111,10 @@ Grid = class Grid { return this.findAllIndices(func).map((pt) => this.get(pt)) } + count(func) { + return this.findAllIndices(func).length + } + indexOf(val) { return this.findIndex((e) => e == val) } diff --git a/out.js b/out.js index a1ec27f..1faad35 100644 --- a/out.js +++ b/out.js @@ -199,12 +199,12 @@ Pt = Point = class Point { dl() { return this.downleft() } dr() { return this.downright() } - getUnfilteredAdjNeighbors() { return [this.u(), this.l(), this.r(), this.d()] } - getUnfilteredDiagNeighbors() { return [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()] } - getUnfilteredAdjNeighborsIncSelf() { return [this.u(), this.l(), this, this.r(), this.d()] } - getUnfilteredDiagNeighborsIncSelf() { return [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()] } + getUnfilteredAdjNeighbors() { return new PointArray(this.u(), this.l(), this.r(), this.d()) } + getUnfilteredDiagNeighbors() { return new PointArray(this.ul(), this.ur(), this.dl(), this.dr()) } + getUnfilteredAllNeighbors() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()) } + getUnfilteredAdjNeighborsIncSelf() { return new PointArray(this.u(), this.l(), this, this.r(), this.d()) } + getUnfilteredDiagNeighborsIncSelf() { return new PointArray(this.ul(), this.ur(), this, this.dl(), 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) } addMut(pt) { @@ -321,6 +321,8 @@ Grid = class Grid { } findIndex(func) { + func = typeof func == "function" ? func : (e) => e == func + for (let y = 0; y < this.height; y++) { for (let x = 0; x < this.width; x++) { let pt = new Point(x, y) @@ -338,8 +340,11 @@ Grid = class Grid { } findAllIndices(func) { + func = typeof func == "function" ? func : (e) => e == func + let points = [].pt this.forEach((e, pt, grid) => func(e, pt, grid) ? points.push(pt) : 0) + return points } @@ -347,6 +352,10 @@ Grid = class Grid { return this.findAllIndices(func).map((pt) => this.get(pt)) } + count(func) { + return this.findAllIndices(func).length + } + indexOf(val) { return this.findIndex((e) => e == val) } diff --git a/pt.js b/pt.js index afcc247..ede753c 100644 --- a/pt.js +++ b/pt.js @@ -27,12 +27,12 @@ Pt = Point = class Point { dl() { return this.downleft() } dr() { return this.downright() } - getUnfilteredAdjNeighbors() { return [this.u(), this.l(), this.r(), this.d()] } - getUnfilteredDiagNeighbors() { return [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()] } - getUnfilteredAdjNeighborsIncSelf() { return [this.u(), this.l(), this, this.r(), this.d()] } - getUnfilteredDiagNeighborsIncSelf() { return [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()] } + getUnfilteredAdjNeighbors() { return new PointArray(this.u(), this.l(), this.r(), this.d()) } + getUnfilteredDiagNeighbors() { return new PointArray(this.ul(), this.ur(), this.dl(), this.dr()) } + getUnfilteredAllNeighbors() { return new PointArray(this.ul(), this.u(), this.ur(), this.l(), this.r(), this.dl(), this.d(), this.dr()) } + getUnfilteredAdjNeighborsIncSelf() { return new PointArray(this.u(), this.l(), this, this.r(), this.d()) } + getUnfilteredDiagNeighborsIncSelf() { return new PointArray(this.ul(), this.ur(), this, this.dl(), 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) } addMut(pt) {