Compare commits

..

3 Commits

Author SHA1 Message Date
9f9b14fb7b Day 24 Part 2 Complete 2024-12-25 15:26:23 +00:00
97a0dad48d Day 24 Part 2 Attempt 2024-12-25 14:07:37 +00:00
b496e480b1 Day 24 Part 2 Attempt 2024-12-25 13:55:42 +00:00

View File

@@ -23,13 +23,21 @@ initStr.split("\n").forEach((s: string) => {
});
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 [val1, op, val2] = inp.split(' ');
gateMap[out] = [val1, op, val2];
if (out.startsWith('z')) {
return [val1, op, val2, out];
});
gates.forEach(([val1, op, val2, out]) => {
if (out[0] === 'z') {
outputWires.push(out);
}
gateMap[out] = [val1, op, val2];
});
function getWireVal(wire: string, trace: Array<string> = []): number {
@@ -56,8 +64,8 @@ function getVal(wire1: string, op: string, wire2: string, trace: Array<string> =
}[op]!();
}
outputWires.sort().reverse();
outputWires.sort();
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
function calculate() {
let outputStr = '';
@@ -68,56 +76,131 @@ function calculate() {
return outputStr;
}
function mistakes(act: string, exp: string): number {
let mistakes = 0;
for (let i = 0; i < act.length; i++) {
if (act[i] !== exp[i]) {
mistakes++;
function swap(wire1: string, wire2: string): void {
const wire1Gate = gateMap[wire1];
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;
}
}
return mistakes;
});
}
let fewestMistakes: number|null = null;
let bestSwap: [string, string]|null = null;
let swapResult: string|null = null;
interface HalfAdder {
x: string,
y: string,
out: string,
carry: string,
sumGate: [string, string, string, string],
carryGate: [string, string, string, string],
}
const wires = Object.keys(gateMap);
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],
}
const expected = (parseInt(xStr, 2) + parseInt(yStr, 2)).toString(2);
const swaps: Array<string> = [];
for (let i = 0; i < wires.length; i++) {
for (let j = i + 1; j < wires.length; j++) {
const wireA = wires[i];
const wireB = wires[j];
const gateA = gateMap[wireA];
const gateB = gateMap[wireB];
gateMap[wireA] = gateB;
gateMap[wireB] = gateA;
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);
});
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);
let actual;
try {
actual = calculate();
} catch (e) {
console.log(`Wrong input found, ${wrongInput} should be ${shouldBe}`)
return gate;
}
const carryGate = getGate('x00', 'y00', 'AND');
const sumGate = getGate('x00', 'y00', 'XOR');
const halfAdder: HalfAdder = {
x: 'x00',
y: 'y00',
out: 'z00',
carry: carryGate[3],
carryGate: carryGate,
sumGate: sumGate,
}
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;
} finally {
gateMap[wireA] = gateA;
gateMap[wireB] = gateB;
}
const mist = mistakes(actual, expected);
const x = out.replace('z', 'x');
const y = out.replace('z', 'y');
if (!fewestMistakes || fewestMistakes > mist) {
fewestMistakes = mist;
bestSwap = [wireA, wireB];
swapResult = actual;
}
}
const adder = getAdder(x, y, carry, out);
console.log(adder, swaps);
carry = adder.carry;
}
console.log(xStr);
console.log(yStr);
console.log(expected);
console.log(calculate());
console.log(fewestMistakes);
console.log(bestSwap);
console.log(swapResult);
swaps.sort();
console.log(swaps.join(','));