2022-18 in js

This commit is contained in:
Julien Dessaux 2022-12-27 22:42:42 +01:00
parent c14d3aac12
commit ece85a2e99
Signed by: adyxax
GPG key ID: F92E51B86E07177E
7 changed files with 2927 additions and 0 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
zig-cache
node_modules
package-lock.json

View file

@ -0,0 +1,44 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:node/recommended"
],
"overrides": [
{
"files": ["*.js"],
"rules": {
"no-constant-condition": "off"
}
}
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"node/no-unsupported-features/es-syntax": [
"error",
{ "ignores": ["modules"] }
]
}
}

View file

@ -0,0 +1,13 @@
2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5

View file

@ -0,0 +1,38 @@
import * as fs from "fs";
function load(filename) {
return fs.readFileSync(filename, "utf8")
.trim()
.split("\n");
}
let example = load("example");
let input = load("input");
function countSides(cube, input) {
let [x, y, z] = cube.split(",").map(n => parseInt(n));
let count = 6;
if (input.includes([x+1, y, z].join(","))) count--;
if (input.includes([x-1, y, z].join(","))) count--;
if (input.includes([x, y+1, z].join(","))) count--;
if (input.includes([x, y-1, z].join(","))) count--;
if (input.includes([x, y, z+1].join(","))) count--;
if (input.includes([x, y, z-1].join(","))) count--;
return count;
}
function solve(input) {
let count = 0;
input.forEach(cube => {
count += countSides(cube, input);
});
return count;
}
const exampleOutput = solve(example);
if (exampleOutput !== 64) {
console.log("Example failed with " + exampleOutput);
process.exit(1); // eslint-disable-line
}
console.log(solve(input));

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"eslint": "^8.30.0",
"eslint-plugin-node": "^11.1.0",
"jslint": "^0.12.1"
}
}

View file

@ -0,0 +1,63 @@
import * as fs from "fs";
function load(filename) {
return fs.readFileSync(filename, "utf8")
.trim()
.split("\n");
}
let example = load("example");
let input = load("input");
function countSides(x, y, z, input) {
let count = 0;
if (input.includes([x+1, y, z].join(","))) count++;
if (input.includes([x-1, y, z].join(","))) count++;
if (input.includes([x, y+1, z].join(","))) count++;
if (input.includes([x, y-1, z].join(","))) count++;
if (input.includes([x, y, z+1].join(","))) count++;
if (input.includes([x, y, z-1].join(","))) count++;
return count;
}
function solve(input) {
// compute the boundaries of the playing field
let min = Infinity;
let max = -Infinity;
input.forEach(line => {
let [x, y, z] = line.split(",").map(n => parseInt(n));
min = Math.min(min, x, y, z);
max = Math.max(max, x, y, z);
})
min -=1;
max +=1;
// start with an edge and fill up the space
let count = 0;
let queue = [[min, min, min]];
let visited = new Set();
while (queue.length > 0) {
const elt = queue.shift();
const eltstr = elt.join(",");
const [x, y, z] = elt;
if (x < min || x > max || y < min || y > max || z < min || z > max) continue;
if (visited.has(eltstr)) continue
if (input.includes(eltstr)) continue;
visited.add(eltstr);
count += countSides(x, y, z, input);
queue.push([x+1, y, z]);
queue.push([x-1, y, z]);
queue.push([x, y+1, z]);
queue.push([x, y-1, z]);
queue.push([x, y, z+1]);
queue.push([x, y, z-1]);
}
return count;
}
const exampleOutput = solve(example);
if (exampleOutput !== 58) {
console.log("Example failed with " + exampleOutput);
process.exit(1); // eslint-disable-line
}
console.log(solve(input));