Skip to content

Commit

Permalink
2021.22.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kzlsakal committed Dec 22, 2021
1 parent 88073dd commit 5c12914
Show file tree
Hide file tree
Showing 2 changed files with 457 additions and 0 deletions.
37 changes: 37 additions & 0 deletions challenges/2021/22.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Part 1
const solvePuzzle1 = (input) => {
input = parseInput(input);
const cuboids = parseCuboids(input);
const on = new Set();

cuboids.forEach((cuboid, idx) => {
const [x1, x2, y1, y2, z1, z2] = cuboid;
if (Math.max(...[x1, x2, y1, y2, z1, z2].map(Math.abs)) <= 50) {
for (let x = x1; x <= x2; x++) {
for (let y = y1; y <= y2; y++) {
for (let z = z1; z <= z2; z++) {
if (input[idx][1] === "n") {
on.add(`${x},${y},${z}`);
} else {
on.delete(`${x},${y},${z}`);
}
}
}
}
}
});

return on.size;
};

const parseInput = (input) => input.split("\n");
const parseCuboids = (input) =>
input.map((l) =>
l
.split(",")
.map((c) => c.split("=")[1].split(".."))
.flat()
.map(Number)
);

require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle1);
Loading

0 comments on commit 5c12914

Please sign in to comment.