Compare commits
2 Commits
4574a0f268
...
fdc6260055
| Author | SHA1 | Date | |
|---|---|---|---|
| fdc6260055 | |||
| b9dd983a89 |
59
2024/22/index.ts
Normal file
59
2024/22/index.ts
Normal 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
30
2024/23/index.ts
Normal 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);
|
||||
54
2024/24/index.ts
Normal file
54
2024/24/index.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||
|
||||
const [initStr, gatesStr] = input.split("\n\n");
|
||||
|
||||
const valMap: { [key: string]: number }= {};
|
||||
const gateMap: { [key: string]: [string, string, string] } = {};
|
||||
|
||||
const outputWires: Array<string> = [];
|
||||
|
||||
initStr.split("\n").forEach((s: string) => {
|
||||
const [wire, val] = s.split(': ');
|
||||
valMap[wire] = parseInt(val, 2);
|
||||
});
|
||||
|
||||
gatesStr.split("\n").forEach((s: string) => {
|
||||
const [inp, out] = s.split(' -> ');
|
||||
const [val1, op, val2] = inp.split(' ');
|
||||
gateMap[out] = [val1, op, val2];
|
||||
if (out.startsWith('z')) {
|
||||
outputWires.push(out);
|
||||
}
|
||||
});
|
||||
|
||||
function getWireVal(wire: string): number {
|
||||
if (Object.hasOwn(valMap, wire)) {
|
||||
return valMap[wire];
|
||||
}
|
||||
const val = getVal(...gateMap[wire])
|
||||
valMap[wire] = val;
|
||||
return val;
|
||||
}
|
||||
|
||||
function getVal(wire1: string, op: string, wire2: string): number {
|
||||
const val1 = getWireVal(wire1);
|
||||
const val2 = getWireVal(wire2);
|
||||
|
||||
return {
|
||||
AND: () => val1 & val2,
|
||||
OR: () => val1 | val2,
|
||||
XOR: () => val1 ^ val2,
|
||||
}[op]!();
|
||||
}
|
||||
|
||||
outputWires.sort().reverse();
|
||||
|
||||
let outputStr = '';
|
||||
|
||||
outputWires.forEach((wire) => {
|
||||
outputStr += getWireVal(wire);
|
||||
});
|
||||
|
||||
console.log(outputStr, parseInt(outputStr, 2));
|
||||
123
2024/24/index2.ts
Normal file
123
2024/24/index2.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||
|
||||
const [initStr, gatesStr] = input.split("\n\n");
|
||||
|
||||
const originalValMap: { [key: string]: number }= {};
|
||||
let valMap: { [key: string]: number } = {};
|
||||
const gateMap: { [key: string]: [string, string, string] } = {};
|
||||
|
||||
let xStr = '';
|
||||
let yStr = '';
|
||||
const outputWires: Array<string> = [];
|
||||
|
||||
initStr.split("\n").forEach((s: string) => {
|
||||
const [wire, val] = s.split(': ');
|
||||
originalValMap[wire] = parseInt(val, 2);
|
||||
if (wire.startsWith('x')) {
|
||||
xStr += val;
|
||||
} else {
|
||||
yStr += val;
|
||||
}
|
||||
});
|
||||
valMap = {...originalValMap};
|
||||
|
||||
gatesStr.split("\n").forEach((s: string) => {
|
||||
const [inp, out] = s.split(' -> ');
|
||||
const [val1, op, val2] = inp.split(' ');
|
||||
gateMap[out] = [val1, op, val2];
|
||||
if (out.startsWith('z')) {
|
||||
outputWires.push(out);
|
||||
}
|
||||
});
|
||||
|
||||
function getWireVal(wire: string, trace: Array<string> = []): number {
|
||||
if (Object.hasOwn(valMap, wire)) {
|
||||
return valMap[wire];
|
||||
}
|
||||
const [wire1, op, wire2] = gateMap[wire];
|
||||
if (trace.includes(wire1) || trace.includes(wire2)) {
|
||||
throw Error('Cycle');
|
||||
}
|
||||
const val = getVal(wire1, op, wire2, [...trace, wire1, wire2]);
|
||||
valMap[wire] = val;
|
||||
return val;
|
||||
}
|
||||
|
||||
function getVal(wire1: string, op: string, wire2: string, trace: Array<string> = []): number {
|
||||
const val1 = getWireVal(wire1, trace);
|
||||
const val2 = getWireVal(wire2, trace);
|
||||
|
||||
return {
|
||||
AND: () => val1 & val2,
|
||||
OR: () => val1 | val2,
|
||||
XOR: () => val1 ^ val2,
|
||||
}[op]!();
|
||||
}
|
||||
|
||||
outputWires.sort().reverse();
|
||||
|
||||
|
||||
function calculate() {
|
||||
let outputStr = '';
|
||||
valMap = {...originalValMap};
|
||||
outputWires.forEach((wire) => {
|
||||
outputStr += getWireVal(wire);
|
||||
});
|
||||
return outputStr;
|
||||
}
|
||||
|
||||
function mistakes(act: string, exp: string): number {
|
||||
let mistakes = 0;
|
||||
for (let i = 0; i < act.length; i++) {
|
||||
if (act[i] !== exp[i]) {
|
||||
mistakes++;
|
||||
}
|
||||
}
|
||||
return mistakes;
|
||||
}
|
||||
|
||||
let fewestMistakes: number|null = null;
|
||||
let bestSwap: [string, string]|null = null;
|
||||
let swapResult: string|null = null;
|
||||
|
||||
const wires = Object.keys(gateMap);
|
||||
|
||||
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
|
||||
|
||||
for (let i = 0; i < wires.length; i++) {
|
||||
for (let j = i + 1; j < wires.length; j++) {
|
||||
const wireA = wires[i];
|
||||
const wireB = wires[j];
|
||||
const gateA = gateMap[wireA];
|
||||
const gateB = gateMap[wireB];
|
||||
gateMap[wireA] = gateB;
|
||||
gateMap[wireB] = gateA;
|
||||
|
||||
let actual;
|
||||
try {
|
||||
actual = calculate();
|
||||
} catch (e) {
|
||||
continue;
|
||||
} finally {
|
||||
gateMap[wireA] = gateA;
|
||||
gateMap[wireB] = gateB;
|
||||
}
|
||||
const mist = mistakes(actual, expected);
|
||||
|
||||
if (!fewestMistakes || fewestMistakes > mist) {
|
||||
fewestMistakes = mist;
|
||||
bestSwap = [wireA, wireB];
|
||||
swapResult = actual;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(xStr);
|
||||
console.log(yStr);
|
||||
console.log(expected);
|
||||
console.log(calculate());
|
||||
console.log(fewestMistakes);
|
||||
console.log(bestSwap);
|
||||
console.log(swapResult);
|
||||
Reference in New Issue
Block a user