Day 24 Part 2 Attempt
This commit is contained in:
@@ -35,24 +35,6 @@ const gates: Array<[string, string, string, string]> = gatesStr.split("\n").map(
|
||||
});
|
||||
gates.forEach(([val1, op, val2, out]) => {
|
||||
gateMap[out] = [val1, op, val2];
|
||||
if (out.startsWith('z')) {
|
||||
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 {
|
||||
@@ -80,6 +62,7 @@ function getVal(wire1: string, op: string, wire2: string, trace: Array<string> =
|
||||
}
|
||||
|
||||
outputWires.sort().reverse();
|
||||
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
|
||||
|
||||
function calculate() {
|
||||
let outputStr = '';
|
||||
@@ -90,15 +73,6 @@ function calculate() {
|
||||
return outputStr;
|
||||
}
|
||||
|
||||
function getPermutations(arr: Array<string>): Array<Array<string>> {
|
||||
if (arr.length === 1) {
|
||||
return [arr];
|
||||
}
|
||||
return arr.flatMap((s, i) => {
|
||||
return getPermutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map((p) => [s, ...p]);
|
||||
});
|
||||
}
|
||||
|
||||
function swap(wire1: string, wire2: string): void {
|
||||
const wire1Gate = gateMap[wire1];
|
||||
const wire2Gate = gateMap[wire2];
|
||||
@@ -106,49 +80,44 @@ function swap(wire1: string, wire2: string): void {
|
||||
gateMap[wire2] = wire1Gate;
|
||||
}
|
||||
|
||||
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
|
||||
|
||||
const zOutputsToSwap = badOuts.filter((o) => o[0] === 'z');
|
||||
const otherOutputsToSwap = badOuts.filter((o) => o[0] !== 'z');
|
||||
|
||||
for (let perm of getPermutations(zOutputsToSwap)) {
|
||||
swap(perm[0], otherOutputsToSwap[0]);
|
||||
swap(perm[1], otherOutputsToSwap[1]);
|
||||
swap(perm[2], otherOutputsToSwap[2]);
|
||||
|
||||
for (let i = 0; i < gates.length - 1; i++) {
|
||||
for (let j = i + 1; j < gates.length; j++) {
|
||||
swap(gates[i][3], gates[j][3]);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
interface HalfAdder {
|
||||
x: string,
|
||||
y: string,
|
||||
out: string,
|
||||
carry: string,
|
||||
sumGate: [string, string, string, string],
|
||||
carryGate: [string, string, string, string],
|
||||
}
|
||||
|
||||
swap(perm[0], otherOutputsToSwap[0]);
|
||||
swap(perm[1], otherOutputsToSwap[1]);
|
||||
swap(perm[2], otherOutputsToSwap[2]);
|
||||
interface FullAdder {
|
||||
x: string,
|
||||
y: string,
|
||||
carryIn: string,
|
||||
out: string,
|
||||
carry:string,
|
||||
halfSumGate: [string, string, string, string],
|
||||
fullSumGate: [string, string, string, string],
|
||||
sumCarryGate: [string, string, string, string],
|
||||
inputCarryGate: [string, string, string, string],
|
||||
carryGate: [string, string, string, string],
|
||||
}
|
||||
|
||||
/*
|
||||
* 1234
|
||||
* 4123
|
||||
* 3412
|
||||
* 2341
|
||||
* 4321
|
||||
* 1432
|
||||
* 2143
|
||||
* 3214
|
||||
* 1324
|
||||
* 4132
|
||||
*/
|
||||
function getGate(a: string, b: string, operator: string): undefined|[string, string, string, string] {
|
||||
return gates.find(([val1, op, val2]) => {
|
||||
return op === operator
|
||||
&& ((a === val1 && b === val2) || a === val2 && b === val1);
|
||||
});
|
||||
}
|
||||
|
||||
const carryGate = getGate('x00', 'y00', 'AND');
|
||||
const sumGate = getGate('x00', 'y00', 'OR');
|
||||
const halfAdder: HalfAdder = {
|
||||
x: 'x00',
|
||||
y: 'y00',
|
||||
out: 'z00',
|
||||
carry: carryGate![3],
|
||||
carryGate: carryGate!,
|
||||
sumGate: sumGate!,
|
||||
}
|
||||
|
||||
console.log(halfAdder);
|
||||
Reference in New Issue
Block a user