2022-18 in js
This commit is contained in:
parent
c14d3aac12
commit
ece85a2e99
7 changed files with 2927 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
zig-cache
|
||||
node_modules
|
||||
package-lock.json
|
||||
|
|
44
2022/18-Boiling-Boulders/.eslintrc.json
Normal file
44
2022/18-Boiling-Boulders/.eslintrc.json
Normal 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"] }
|
||||
]
|
||||
}
|
||||
}
|
13
2022/18-Boiling-Boulders/example
Normal file
13
2022/18-Boiling-Boulders/example
Normal 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
|
38
2022/18-Boiling-Boulders/first.js
Normal file
38
2022/18-Boiling-Boulders/first.js
Normal 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));
|
2759
2022/18-Boiling-Boulders/input
Normal file
2759
2022/18-Boiling-Boulders/input
Normal file
File diff suppressed because it is too large
Load diff
8
2022/18-Boiling-Boulders/package.json
Normal file
8
2022/18-Boiling-Boulders/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"eslint": "^8.30.0",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"jslint": "^0.12.1"
|
||||
}
|
||||
}
|
63
2022/18-Boiling-Boulders/second.js
Normal file
63
2022/18-Boiling-Boulders/second.js
Normal 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));
|
Loading…
Add table
Reference in a new issue