Day 24 Part 2 Attempt

This commit is contained in:
2024-12-25 13:55:42 +00:00
parent fdc6260055
commit b496e480b1

View File

@@ -23,13 +23,36 @@ initStr.split("\n").forEach((s: string) => {
}); });
valMap = {...originalValMap}; valMap = {...originalValMap};
gatesStr.split("\n").forEach((s: string) => { const carries: Array<string> = [];
const halfAdds: Array<string> = [];
const badOuts: Array<string> = [];
const badAnds: Array<string> = [];
const gates: Array<[string, string, string, string]> = gatesStr.split("\n").map((s: string) => {
const [inp, out] = s.split(' -> '); const [inp, out] = s.split(' -> ');
const [val1, op, val2] = inp.split(' '); const [val1, op, val2] = inp.split(' ');
return [val1, op, val2, out];
});
gates.forEach(([val1, op, val2, out]) => {
gateMap[out] = [val1, op, val2]; gateMap[out] = [val1, op, val2];
if (out.startsWith('z')) { if (out.startsWith('z')) {
outputWires.push(out); outputWires.push(out);
} }
if ((op === 'XOR' && !val1.startsWith('x') && !val1.startsWith('y')) || (op === 'XOR' && (val1 === 'x00' || val1 === 'y00'))) {
if (!out.startsWith('z')) {
badOuts.push(out);
}
} else if (out.startsWith('z')) {
badOuts.push(out);
}
if (op === 'AND') {
for (let [val1, op, val2] of gates) {
if (op !== 'OR' && (val1 === out || val2 === out)) {
badAnds.push(out);
break;
}
}
}
}); });
function getWireVal(wire: string, trace: Array<string> = []): number { function getWireVal(wire: string, trace: Array<string> = []): number {
@@ -58,7 +81,6 @@ function getVal(wire1: string, op: string, wire2: string, trace: Array<string> =
outputWires.sort().reverse(); outputWires.sort().reverse();
function calculate() { function calculate() {
let outputStr = ''; let outputStr = '';
valMap = {...originalValMap}; valMap = {...originalValMap};
@@ -68,56 +90,65 @@ function calculate() {
return outputStr; return outputStr;
} }
function mistakes(act: string, exp: string): number { function getPermutations(arr: Array<string>): Array<Array<string>> {
let mistakes = 0; if (arr.length === 1) {
for (let i = 0; i < act.length; i++) { return [arr];
if (act[i] !== exp[i]) {
mistakes++;
}
} }
return mistakes; return arr.flatMap((s, i) => {
return getPermutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map((p) => [s, ...p]);
});
} }
let fewestMistakes: number|null = null; function swap(wire1: string, wire2: string): void {
let bestSwap: [string, string]|null = null; const wire1Gate = gateMap[wire1];
let swapResult: string|null = null; const wire2Gate = gateMap[wire2];
gateMap[wire1] = wire2Gate;
const wires = Object.keys(gateMap); gateMap[wire2] = wire1Gate;
}
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2); const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
for (let i = 0; i < wires.length; i++) { const zOutputsToSwap = badOuts.filter((o) => o[0] === 'z');
for (let j = i + 1; j < wires.length; j++) { const otherOutputsToSwap = badOuts.filter((o) => o[0] !== 'z');
const wireA = wires[i];
const wireB = wires[j];
const gateA = gateMap[wireA];
const gateB = gateMap[wireB];
gateMap[wireA] = gateB;
gateMap[wireB] = gateA;
let actual; for (let perm of getPermutations(zOutputsToSwap)) {
try { swap(perm[0], otherOutputsToSwap[0]);
actual = calculate(); swap(perm[1], otherOutputsToSwap[1]);
} catch (e) { swap(perm[2], otherOutputsToSwap[2]);
continue;
} finally {
gateMap[wireA] = gateA;
gateMap[wireB] = gateB;
}
const mist = mistakes(actual, expected);
if (!fewestMistakes || fewestMistakes > mist) { for (let i = 0; i < gates.length - 1; i++) {
fewestMistakes = mist; for (let j = i + 1; j < gates.length; j++) {
bestSwap = [wireA, wireB]; swap(gates[i][3], gates[j][3]);
swapResult = actual; try {
const output = calculate();
if (output === expected) {
const swaps = [...perm, ...otherOutputsToSwap, gates[i][3], gates[j][3]];
swaps.sort();
console.log(swaps);
break;
}
} catch (e) {
//
} finally {
swap(gates[i][3], gates[j][3]);
}
} }
} }
swap(perm[0], otherOutputsToSwap[0]);
swap(perm[1], otherOutputsToSwap[1]);
swap(perm[2], otherOutputsToSwap[2]);
} }
console.log(xStr); /*
console.log(yStr); * 1234
console.log(expected); * 4123
console.log(calculate()); * 3412
console.log(fewestMistakes); * 2341
console.log(bestSwap); * 4321
console.log(swapResult); * 1432
* 2143
* 3214
* 1324
* 4132
*/