2022-21 part 1 in js
This commit is contained in:
parent
b7a31cc688
commit
7a7a788a11
7 changed files with 2640 additions and 6 deletions
|
@ -65,9 +65,9 @@ class File {
|
|||
function load(filename) {
|
||||
return new File(
|
||||
fs.readFileSync(filename, "utf8")
|
||||
.trim()
|
||||
.split("\n")
|
||||
.map(line => parseInt(line))
|
||||
.trim()
|
||||
.split("\n")
|
||||
.map(line => parseInt(line))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -65,9 +65,9 @@ class File {
|
|||
function load(filename) {
|
||||
return new File(
|
||||
fs.readFileSync(filename, "utf8")
|
||||
.trim()
|
||||
.split("\n")
|
||||
.map(line => parseInt(line))
|
||||
.trim()
|
||||
.split("\n")
|
||||
.map(line => parseInt(line))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
44
2022/21-Monkey-Math/.eslintrc.json
Normal file
44
2022/21-Monkey-Math/.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"] }
|
||||
]
|
||||
}
|
||||
}
|
15
2022/21-Monkey-Math/example
Normal file
15
2022/21-Monkey-Math/example
Normal file
|
@ -0,0 +1,15 @@
|
|||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
45
2022/21-Monkey-Math/first.js
Normal file
45
2022/21-Monkey-Math/first.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
import * as fs from "fs";
|
||||
|
||||
const simple = /(\w+): (\d+)/;
|
||||
const operation = /(\w+): (\w+) ([+\-*/]) (\w+)/;
|
||||
|
||||
function load(filename) {
|
||||
let res = {};
|
||||
fs.readFileSync(filename, "utf8")
|
||||
.trim()
|
||||
.split("\n")
|
||||
.forEach(line => {
|
||||
const s = line.match(simple);
|
||||
if (s) {
|
||||
res[s[1]] = function() { return parseInt(s[2]); };
|
||||
} else {
|
||||
const o = line.match(operation);
|
||||
res[o[1]] = function () {
|
||||
let left = res[o[2]]();
|
||||
let right = res[o[4]]();
|
||||
switch(o[3]) {
|
||||
case "+": return left + right;
|
||||
case "-": return left - right;
|
||||
case "*": return left * right;
|
||||
case "/": return left / right;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
let example = load("example");
|
||||
let input = load("input");
|
||||
|
||||
function solve(input) {
|
||||
return input["root"]();
|
||||
}
|
||||
|
||||
const exampleOutput = solve(example);
|
||||
if (exampleOutput !== 152) {
|
||||
console.log("Example failed with " + exampleOutput);
|
||||
process.exit(1); // eslint-disable-line
|
||||
}
|
||||
|
||||
console.log(solve(input));
|
2519
2022/21-Monkey-Math/input
Normal file
2519
2022/21-Monkey-Math/input
Normal file
File diff suppressed because it is too large
Load diff
11
2022/21-Monkey-Math/package.json
Normal file
11
2022/21-Monkey-Math/package.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"type": "module",
|
||||
"engines": {
|
||||
"node": ">=18.10.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"eslint": "^8.30.0",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"jslint": "^0.12.1"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue