.
This commit is contained in:
66
2021/22.js
Normal file
66
2021/22.js
Normal file
@@ -0,0 +1,66 @@
|
||||
class Cuboid {
|
||||
constructor(sgn, xs, xe, ys, ye, zs, ze) {
|
||||
this.sgn = sgn
|
||||
this.xs = Math.min(xs, xe)
|
||||
this.xe = Math.max(xs, xe)
|
||||
this.ys = Math.min(ys, ye)
|
||||
this.ye = Math.max(ys, ye)
|
||||
this.zs = Math.min(zs, ze)
|
||||
this.ze = Math.max(zs, ze)
|
||||
}
|
||||
|
||||
vol() { return (this.xe - this.xs + 1) * (this.ye - this.ys + 1) * (this.ze - this.zs + 1) }
|
||||
|
||||
int(that) {
|
||||
if (that.xe < this.xs || this.xe < that.xs) return
|
||||
if (that.ye < this.ys || this.ye < that.ys) return
|
||||
if (that.ze < this.zs || this.ze < that.zs) return
|
||||
|
||||
let sgn = -that.sgn
|
||||
|
||||
let xs = Math.max(this.xs, that.xs)
|
||||
let xe = Math.min(this.xe, that.xe)
|
||||
let ys = Math.max(this.ys, that.ys)
|
||||
let ye = Math.min(this.ye, that.ye)
|
||||
let zs = Math.max(this.zs, that.zs)
|
||||
let ze = Math.min(this.ze, that.ze)
|
||||
|
||||
return new Cuboid(sgn, xs, xe, ys, ye, zs, ze)
|
||||
}
|
||||
}
|
||||
|
||||
function day20(input, part2) {
|
||||
let lines = input.split`\n`
|
||||
|
||||
let cuboids = []
|
||||
|
||||
for (let line of lines) {
|
||||
let data = line.match(/^(.+) x=(.+)\.\.(.+),y=(.+)\.\.(.+),z=(.+)\.\.(.+)$/)
|
||||
let cuboid = new Cuboid(data[1] == "on", +data[2], +data[3], +data[4], +data[5], +data[6], +data[7])
|
||||
|
||||
if (!part2 && (
|
||||
cuboid.xs < -50 || cuboid.xe > 50 ||
|
||||
cuboid.ys < -50 || cuboid.ye > 50 ||
|
||||
cuboid.zs < -50 || cuboid.ze > 50)) {
|
||||
continue
|
||||
}
|
||||
|
||||
let toAdd = [cuboid]
|
||||
|
||||
for (let otherCuboid of cuboids) {
|
||||
let intersection = cuboid.int(otherCuboid)
|
||||
|
||||
if (intersection) {
|
||||
toAdd.push(intersection)
|
||||
}
|
||||
}
|
||||
|
||||
cuboids.push(...toAdd)
|
||||
}
|
||||
|
||||
return cuboids.map((e) => e.vol() * e.sgn).sum()
|
||||
}
|
||||
|
||||
if (typeof window == "undefined") {
|
||||
module.exports = day20
|
||||
}
|
||||
2
out.js
2
out.js
@@ -1244,7 +1244,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") {
|
||||
|
||||
const year = "2021"
|
||||
|
||||
for (let i = +process.argv[3] || 1; i <= 21; i++) {
|
||||
for (let i = +process.argv[3] || 1; i <= 22; i++) {
|
||||
const func = require(`./${year}/${i}.js`)
|
||||
const input = fs.readFileSync(`./${year}/inputs/${i}`, "utf8")
|
||||
const answers = fs.readFileSync(`./${year}/answers/${i}`, "utf8").split("\n-----\n")
|
||||
|
||||
2
test.js
2
test.js
@@ -3,7 +3,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") {
|
||||
|
||||
const year = "2021"
|
||||
|
||||
for (let i = +process.argv[3] || 1; i <= 21; i++) {
|
||||
for (let i = +process.argv[3] || 1; i <= 22; i++) {
|
||||
const func = require(`./${year}/${i}.js`)
|
||||
const input = fs.readFileSync(`./${year}/inputs/${i}`, "utf8")
|
||||
const answers = fs.readFileSync(`./${year}/answers/${i}`, "utf8").split("\n-----\n")
|
||||
|
||||
Reference in New Issue
Block a user