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]) => {
|
gates.forEach(([val1, op, val2, out]) => {
|
||||||
gateMap[out] = [val1, op, val2];
|
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 {
|
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();
|
outputWires.sort().reverse();
|
||||||
|
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
|
||||||
|
|
||||||
function calculate() {
|
function calculate() {
|
||||||
let outputStr = '';
|
let outputStr = '';
|
||||||
@@ -90,15 +73,6 @@ function calculate() {
|
|||||||
return outputStr;
|
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 {
|
function swap(wire1: string, wire2: string): void {
|
||||||
const wire1Gate = gateMap[wire1];
|
const wire1Gate = gateMap[wire1];
|
||||||
const wire2Gate = gateMap[wire2];
|
const wire2Gate = gateMap[wire2];
|
||||||
@@ -106,49 +80,44 @@ function swap(wire1: string, wire2: string): void {
|
|||||||
gateMap[wire2] = wire1Gate;
|
gateMap[wire2] = wire1Gate;
|
||||||
}
|
}
|
||||||
|
|
||||||
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
|
interface HalfAdder {
|
||||||
|
x: string,
|
||||||
const zOutputsToSwap = badOuts.filter((o) => o[0] === 'z');
|
y: string,
|
||||||
const otherOutputsToSwap = badOuts.filter((o) => o[0] !== 'z');
|
out: string,
|
||||||
|
carry: string,
|
||||||
for (let perm of getPermutations(zOutputsToSwap)) {
|
sumGate: [string, string, string, string],
|
||||||
swap(perm[0], otherOutputsToSwap[0]);
|
carryGate: [string, string, string, string],
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
swap(perm[0], otherOutputsToSwap[0]);
|
interface FullAdder {
|
||||||
swap(perm[1], otherOutputsToSwap[1]);
|
x: string,
|
||||||
swap(perm[2], otherOutputsToSwap[2]);
|
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],
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
function getGate(a: string, b: string, operator: string): undefined|[string, string, string, string] {
|
||||||
* 1234
|
return gates.find(([val1, op, val2]) => {
|
||||||
* 4123
|
return op === operator
|
||||||
* 3412
|
&& ((a === val1 && b === val2) || a === val2 && b === val1);
|
||||||
* 2341
|
});
|
||||||
* 4321
|
}
|
||||||
* 1432
|
|
||||||
* 2143
|
const carryGate = getGate('x00', 'y00', 'AND');
|
||||||
* 3214
|
const sumGate = getGate('x00', 'y00', 'OR');
|
||||||
* 1324
|
const halfAdder: HalfAdder = {
|
||||||
* 4132
|
x: 'x00',
|
||||||
*/
|
y: 'y00',
|
||||||
|
out: 'z00',
|
||||||
|
carry: carryGate![3],
|
||||||
|
carryGate: carryGate!,
|
||||||
|
sumGate: sumGate!,
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(halfAdder);
|
||||||
Reference in New Issue
Block a user