Compare commits
No commits in common. "fe47a7c682d6f3d117426d00c65027d341f1e72d" and "efacb442546d703f6ab19ede4c20376cc81fbd69" have entirely different histories.
fe47a7c682
...
efacb44254
7 changed files with 0 additions and 8171 deletions
|
@ -1,7 +0,0 @@
|
|||
1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
109
2022/20/index.js
109
2022/20/index.js
|
@ -1,109 +0,0 @@
|
|||
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 node = {
|
||||
val: arr[i],
|
||||
par
|
||||
}
|
||||
order.push(node);
|
||||
if(i < arr.length - 1) {
|
||||
node.next = createNode(arr, order, first || node, node, i+1)
|
||||
return node;
|
||||
}
|
||||
first.par = node;
|
||||
node.next = first;
|
||||
node.last = true;
|
||||
return node;
|
||||
};
|
||||
|
||||
const print = (node, first) => node === first ? [] : [node.val, ...print(node.next, first || node)];
|
||||
|
||||
const key = 811589153;
|
||||
|
||||
const main = async () => {
|
||||
const input = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n").map(x => parseInt(x) * key);
|
||||
|
||||
const order = [];
|
||||
let head = createNode(input, order, 0);
|
||||
|
||||
const shiftIntoOffset = (node, offset) => {
|
||||
if(offset === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let replace = node.next;
|
||||
node.par.next = replace;
|
||||
replace.par = node.par;
|
||||
if(node === head) {
|
||||
head = node.next;
|
||||
}
|
||||
|
||||
for(let i = 0; i < offset; ++i) {
|
||||
replace = replace.next;
|
||||
}
|
||||
if(replace === head) {
|
||||
head = node;
|
||||
}
|
||||
replace.par.next = node;
|
||||
node.par = replace.par;
|
||||
node.next = replace;
|
||||
replace.par = node;
|
||||
return;
|
||||
}
|
||||
|
||||
const mix = () => {
|
||||
for(let i = 0; i < order.length; i++) {
|
||||
const node = order[i];
|
||||
|
||||
const shiftBy = modulo(node.val, input.length - 1);
|
||||
|
||||
if(shiftBy !== 0) {
|
||||
shiftIntoOffset(node, shiftBy);
|
||||
}
|
||||
}
|
||||
};
|
||||
for(let i = 0; i < 10; ++i) {
|
||||
mix();
|
||||
}
|
||||
|
||||
console.log("Mixed!");
|
||||
const getAnswer = () => {
|
||||
const answer = [];
|
||||
let node = order.find(x => 0 === x.val);
|
||||
let i = 0;
|
||||
while(answer.length < 3) {
|
||||
if(i === 1000 % input.length
|
||||
|| i === 2000 % input.length
|
||||
|| i === 3000 % input.length
|
||||
) {
|
||||
answer.push(node.val);
|
||||
}
|
||||
node = node.next;
|
||||
++i;
|
||||
}
|
||||
return answer;
|
||||
};
|
||||
console.log("Answer: ", sum(getAnswer()));
|
||||
}
|
||||
|
||||
|
||||
main();
|
5000
2022/20/input.txt
5000
2022/20/input.txt
File diff suppressed because it is too large
Load diff
|
@ -1,7 +0,0 @@
|
|||
1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
|
@ -1,69 +0,0 @@
|
|||
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();
|
|
@ -1,102 +0,0 @@
|
|||
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
2877
2022/21/input.txt
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue