Day 22 and 23

This commit is contained in:
2024-12-23 10:52:00 +00:00
parent 4574a0f268
commit b9dd983a89
2 changed files with 89 additions and 0 deletions

59
2024/22/index.ts Normal file
View File

@@ -0,0 +1,59 @@
const fs = require('fs');
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
function createSecret(secret: bigint) {
secret = ((secret << 6n) ^ secret) % 16777216n;
secret = ((secret >> 5n) ^ secret) % 16777216n;
return ((secret << 11n) ^ secret) % 16777216n;
}
let sum = 0n;
const changeMap: { [key: string]: Array<number> } = {};
input.split("\n").forEach((s: string, index: number) => {
let seed = BigInt(parseInt(s));
let seedStr = ''+seed;
const changes = [];
for (let i = 0; i < 2000; i++) {
const newSeed = createSecret(seed);
const newSeedStr = ''+newSeed;
const lastDig = parseInt(newSeedStr[newSeedStr.length - 1]);
const diff = lastDig - parseInt(seedStr[seedStr.length - 1]);
changes.push(diff);
if (i > 3) {
changes.shift();
const key = changes.join(',');
if (!changeMap[key]) {
changeMap[key] = [];
}
if (!changeMap[key][index]) {
changeMap[key][index] = lastDig;
}
}
seed = newSeed;
seedStr = newSeedStr;
}
sum += seed;
});
let highest: null|number = null;
let highestSeq: null|string = null;
for (let seq in changeMap) {
const prices = changeMap[seq];
let priceSum = 0;
prices.forEach((p) => {
if (p) {
priceSum += p;
}
});
if (!highest || priceSum > highest) {
highest = priceSum;
highestSeq = seq;
}
}
console.log(sum, highest, highestSeq);

30
2024/23/index.ts Normal file
View File

@@ -0,0 +1,30 @@
const fs = require('fs');
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
const connections: { [key: string]: Array<string> }= {};
input.split("\n").forEach((conn: string) => {
const [c1, c2] = conn.split('-');
connections[c1] = [c2, ...(connections[c1] || [])]
connections[c2] = [c1, ...(connections[c2] || [])]
});
const tCs = new Set;
for (let key in connections) {
const conns = connections[key];
if (key.startsWith('t')) {
conns.forEach((c2) => {
connections[c2].forEach((c3) => {
if (connections[c3].includes(key)) {
const arr = [key, c2, c3];
arr.sort();
tCs.add(arr.join(','));
}
})
})
}
}
console.log(tCs, tCs.size);