that actually wasn't so bad.

This commit is contained in:
John Shaver 2022-12-21 18:53:33 -08:00
parent 29804ff5c2
commit fe47a7c682
4 changed files with 3055 additions and 0 deletions

7
2022/21/example.txt Normal file
View file

@ -0,0 +1,7 @@
1
2
-3
3
-2
0
4

69
2022/21/index.js Normal file
View file

@ -0,0 +1,69 @@
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const modulo = (n, m) => ((n % m + m) % m);
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once('data', data => {
const byteArray = [...data]
if (byteArray.length > 0 && byteArray[0] === 3) {
console.log('^C')
process.exit(1)
}
process.stdin.setRawMode(false)
resolve()
}))
}
const coords = ([x, y, z]) => `${x},${y},${z}`;
const createNode = (arr, order, first, par, i = 0) => {
};
const runOp = (a, op, b) => {
switch(op) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
}
throw(Error(`Op not found! - ${op}`));
}
const inflateMonkeys = (monkeys, name) => {
const command = monkeys[name];
const number = parseInt(command);
if(!isNaN(number)) {
return number;
}
const [_, a, op, b] = command.match(/([a-z]+) (.) ([a-z]+)/);
return runOp(inflateMonkeys(monkeys, a),
op,
inflateMonkeys(monkeys, b));
};
const main = async () => {
const monkeys = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n")
.reduce((monkeys, row) => {
const [_, name, command] = row.match(/([a-z]+): (.*)/);
monkeys[name] = command;
return monkeys;
}, {});;
const root = inflateMonkeys(monkeys, "root");
console.log("root: ", root);
}
main();

102
2022/21/index2.js Normal file
View file

@ -0,0 +1,102 @@
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const modulo = (n, m) => ((n % m + m) % m);
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once('data', data => {
const byteArray = [...data]
if (byteArray.length > 0 && byteArray[0] === 3) {
console.log('^C')
process.exit(1)
}
process.stdin.setRawMode(false)
resolve()
}))
}
const coords = ([x, y, z]) => `${x},${y},${z}`;
const createNode = (arr, order, first, par, i = 0) => {
};
const runOp = (a, op, b) => {
switch(op) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
}
throw(Error(`Op not found! - ${op}`));
}
const invertOp = (op) => {
switch(op) {
case "+":
return "-";
case "-":
return "+";
case "*":
return "/";
case "/":
return "*";
}
};
const inflateMonkeys = (monkeys, name) => {
if(name === "humn") {
return x => x;
}
const command = monkeys[name];
const number = parseInt(command);
if(!isNaN(number)) {
return number;
}
const [_, aName, op, bName] = command.match(/([a-z]+) (.) ([a-z]+)/);
const a = inflateMonkeys(monkeys, aName);
const b = inflateMonkeys(monkeys, bName);
if(typeof a === "function") {
if(name === "root") {
return a(b);
}
return other => a(runOp(other, invertOp(op), b));
}
if(typeof b === "function") {
if(name === "root") {
return b(a);
}
if(op === "/" || op === "-") {
return other => b(runOp(a, op, other));
}
return other => b(runOp(other, invertOp(op), a));
}
return runOp(a, op, b);
};
const main = async () => {
const monkeys = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n")
.reduce((monkeys, row) => {
const [_, name, command] = row.match(/([a-z]+): (.*)/);
monkeys[name] = command;
return monkeys;
}, {});;
const root = inflateMonkeys(monkeys, "root");
console.log("root: ", root);
}
main();

2877
2022/21/input.txt Normal file

File diff suppressed because it is too large Load diff