Compare commits
2 commits
efacb44254
...
fe47a7c682
Author | SHA1 | Date | |
---|---|---|---|
|
fe47a7c682 | ||
|
29804ff5c2 |
7 changed files with 8171 additions and 0 deletions
7
2022/20/example.txt
Normal file
7
2022/20/example.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
109
2022/20/index.js
Normal file
109
2022/20/index.js
Normal file
|
@ -0,0 +1,109 @@
|
|||
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
Normal file
5000
2022/20/input.txt
Normal file
File diff suppressed because it is too large
Load diff
7
2022/21/example.txt
Normal file
7
2022/21/example.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
69
2022/21/index.js
Normal file
69
2022/21/index.js
Normal 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
102
2022/21/index2.js
Normal 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
2877
2022/21/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue