diff --git a/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb b/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb index f7be3a1..1ac4766 100644 --- a/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb +++ b/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb @@ -9379,12 +9379,17 @@ "\n", "This sequence then repeats.\n", "\n", - "If we look at the printed output, there are some obvious issues. In my data, I can immediately see issues with `z06`, `z11` and `z31`. So these are going to be part of the answer. But what else?" + "If we look at the printed output, there are some obvious issues. In my data, I can immediately see issues with `z06`, `z11` and `z31`. So these are going to be part of the answer. But what else?\n", + "\n", + "Here's how we can solve it:\n", + "\n", + "- We can run verifications against every `z` output, and check it contains the expected set of rules for a ripple adder.\n", + "- " ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -9470,11 +9475,11 @@ " \"\"\" Verify a z value has been properly created. \"\"\"\n", " return verify_output_wire(get_wire_name(\"z\", num), num)\n", "\n", - "def progress():\n", + "def find_first_fail():\n", " i = 0\n", - " \n", " while True:\n", - " if not verify(i): break\n", + " if not verify(i): \n", + " break\n", " i += 1\n", " \n", " return i\n", @@ -9486,15 +9491,21 @@ " op_by_ouput[output] = (left, right, gate)\n", " \n", " swaps = []\n", - "\n", " for _ in tqdm(range(swaps_required)):\n", - " baseline = progress()\n", - " for op_x in op_by_ouput:\n", - " for op_y in op_by_ouput:\n", - " if op_x == op_y: continue\n", + " first_fail = find_first_fail() # E.g. 6 for z06\n", + " for op_x in op_by_ouput: # E.g. fkp\n", + " for op_y in op_by_ouput: # E.g. z06\n", + " if op_x == op_y: \n", + " continue\n", + " \n", + " # Swap these two operations and test if we get more successful verifications\n", " op_by_ouput[op_x], op_by_ouput[op_y] = op_by_ouput[op_y], op_by_ouput[op_x]\n", - " if progress() > baseline:\n", - " break\n", + " new_first_fail = find_first_fail()\n", + " if new_first_fail > first_fail:\n", + " first_fail = new_first_fail\n", + " break # We've swapped this op_x and op_y. Move on to next op_x.\n", + " \n", + " # Unsuccessful, so swap back\n", " op_by_ouput[op_x], op_by_ouput[op_y] = op_by_ouput[op_y], op_by_ouput[op_x]\n", " else:\n", " continue\n",