From 8481c09b525c6838247675289c71d426524e3d7b Mon Sep 17 00:00:00 2001 From: martapanc Date: Wed, 25 Dec 2024 16:22:34 +0100 Subject: [PATCH] 2024D24: part 2 refactor --- 2024/src/2024/day24/day24.ts | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/2024/src/2024/day24/day24.ts b/2024/src/2024/day24/day24.ts index 4920708..5e89825 100644 --- a/2024/src/2024/day24/day24.ts +++ b/2024/src/2024/day24/day24.ts @@ -71,17 +71,30 @@ function findOutput(wires: Map, gates: Map) { function findWiresToSwap(_: Map, gates: Map) { const BIT_LENGTH = 45; const incorrect: string[] = []; + + const findGate = (wire1: string, wire2: string, op: string) => + [...gates.entries()].find(([_, g]) => + ((g.wire1 === wire1 && g.wire2 === wire2) || (g.wire1 === wire2 && g.wire2 === wire1)) && g.op === op + ); + + // Helper function to find a gate by wire connection + const findNextGate = (wire: string) => + [...gates.entries()].find(([_, g]) => g.wire1 === wire || g.wire2 === wire); + for (let i = 0; i < BIT_LENGTH; i++) { const id = i.toString().padStart(2, '0'); - const xor1 = [...gates.entries()].find(([_, g]) => ((g.wire1 === `x${id}` && g.wire2 === `y${id}`) || (g.wire1 === `y${id}` && g.wire2 === `x${id}`)) && g.op === 'XOR'); - const and1 = [...gates.entries()].find(([_, g]) => ((g.wire1 === `x${id}` && g.wire2 === `y${id}`) || (g.wire1 === `y${id}` && g.wire2 === `x${id}`)) && g.op === 'AND'); - const z = [...gates.entries()].find(([key]) => key === `z${id}`); + const wire1 = `x${id}`; + const wire2 = `y${id}`; + + const xorGate = findGate(wire1, wire2, 'XOR'); + const andGate = findGate(wire1, wire2, 'AND'); + const zGateEntry = [...gates.entries()].find(([key]) => key === `z${id}`); - if (xor1 === undefined || and1 === undefined || z === undefined) continue; + if (!xorGate || !andGate || !zGateEntry) continue; - const [xorKey] = xor1; - const [andKey] = and1; - const [zKey, zGate] = z; + const [xorKey] = xorGate; + const [andKey] = andGate; + const [zKey, zGate] = zGateEntry; // All z nodes must be connected to a XOR if (zGate.op !== 'XOR') { @@ -98,7 +111,7 @@ function findWiresToSwap(_: Map, gates: Map) { } // The first XOR must go to XOR or AND - const next = [...gates.entries()].find(([_, g]) => g.wire1 === xorKey || g.wire2 === xorKey); + const next = findNextGate(xorKey); if (next !== undefined) { const [_, nextGate] = next; if (nextGate.op === 'OR') {