From 9f9b14fb7b1ef113b75cee14f0954136dbf3b6cf Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 25 Dec 2024 15:26:23 +0000 Subject: [PATCH] Day 24 Part 2 Complete --- 2024/24/index2.ts | 101 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 9 deletions(-) diff --git a/2024/24/index2.ts b/2024/24/index2.ts index bcc1267..51dbb0d 100644 --- a/2024/24/index2.ts +++ b/2024/24/index2.ts @@ -34,6 +34,9 @@ const gates: Array<[string, string, string, string]> = gatesStr.split("\n").map( return [val1, op, val2, out]; }); gates.forEach(([val1, op, val2, out]) => { + if (out[0] === 'z') { + outputWires.push(out); + } gateMap[out] = [val1, op, val2]; }); @@ -61,7 +64,7 @@ function getVal(wire1: string, op: string, wire2: string, trace: Array = }[op]!(); } -outputWires.sort().reverse(); +outputWires.sort(); const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2); function calculate() { @@ -78,6 +81,13 @@ function swap(wire1: string, wire2: string): void { const wire2Gate = gateMap[wire2]; gateMap[wire1] = wire2Gate; gateMap[wire2] = wire1Gate; + gates.forEach((gate) => { + if (gate[3] === wire1) { + gate[3] = wire2; + } else if (gate[3] === wire2) { + gate[3] = wire1; + } + }); } interface HalfAdder { @@ -102,22 +112,95 @@ interface FullAdder { carryGate: [string, string, string, string], } -function getGate(a: string, b: string, operator: string): undefined|[string, string, string, string] { - return gates.find(([val1, op, val2]) => { +const swaps: Array = []; + +function getGate(a: string, b: string, operator: string): [string, string, string, string] { + const gate = gates.find(([val1, op, val2]) => { return op === operator - && ((a === val1 && b === val2) || a === val2 && b === val1); + && (a === val1 || b === val2 || a === val2 || b === val1); }); + if (!gate) { + throw new Error(`Two bad inputs, could not find gate, ${a}, ${b}`); + } + if ((gate[0] === a && gate[2] === b) || (gate[0] === b && gate[2] === a)) { + return gate; + } + console.log(gate, a, b, operator); + let wrongInput: string; + let shouldBe: string; + if (gate[0] === a) { + wrongInput = b; + shouldBe = gate[2] + } else if (gate[0] === b) { + wrongInput = a; + shouldBe = gate[2] + } if (gate[2] === a) { + wrongInput = b; + shouldBe = gate[0] + } else { + wrongInput = a; + shouldBe = gate[0] + } + swaps.push(wrongInput); + swaps.push(shouldBe); + swap(wrongInput, shouldBe); + + console.log(`Wrong input found, ${wrongInput} should be ${shouldBe}`) + return gate; } const carryGate = getGate('x00', 'y00', 'AND'); -const sumGate = getGate('x00', 'y00', 'OR'); +const sumGate = getGate('x00', 'y00', 'XOR'); const halfAdder: HalfAdder = { x: 'x00', y: 'y00', out: 'z00', - carry: carryGate![3], - carryGate: carryGate!, - sumGate: sumGate!, + carry: carryGate[3], + carryGate: carryGate, + sumGate: sumGate, } -console.log(halfAdder); \ No newline at end of file +console.log(halfAdder); + +function getAdder(x: string, y: string, carryIn: string, out: string): FullAdder { + const halfSumGate = getGate(x, y, 'XOR'); + const fullSumGate = getGate(halfSumGate[3], carryIn, 'XOR'); + if (fullSumGate[3] !== out) { + console.log(`Wrong input found, ${fullSumGate[3]} should be ${out}`) + swaps.push(fullSumGate[3]); + swaps.push(out); + swap(fullSumGate[3], out); + } + const sumCarryGate = getGate(halfSumGate[3], carryIn, 'AND'); + const inputCarryGate = getGate(x, y, 'AND')!; + const carryGate = getGate(sumCarryGate[3], inputCarryGate[3], 'OR'); + return { + x, + y, + carryIn, + out: fullSumGate[3], + carry: carryGate[3], + halfSumGate, + fullSumGate, + sumCarryGate, + inputCarryGate, + carryGate + }; +} + +let carry = halfAdder.carry; + +for (let out of outputWires) { + if (out === 'z00' || out === 'z45') { + continue; + } + const x = out.replace('z', 'x'); + const y = out.replace('z', 'y'); + + const adder = getAdder(x, y, carry, out); + console.log(adder, swaps); + carry = adder.carry; +} + +swaps.sort(); +console.log(swaps.join(',')); \ No newline at end of file