Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Jan 4, 2025
1 parent 98ff211 commit 526decb
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9057,11 +9057,11 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"@dataclass(frozen=True, eq=True)\n",
"@dataclass\n",
"class Operation():\n",
" left_wire: str\n",
" right_wire: str\n",
Expand Down Expand Up @@ -9624,22 +9624,18 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"def swap_ops(ops: dict, op_x: str, op_y: str):\n",
" \"\"\" Swap two operations in the operations dictionary.\n",
" Remember that the operations are stored as a dictionary of { output: Operation }\n",
" and that the Operation also contains the output wire name.\n",
" So we need to create a new Operation with the new output wire name,\n",
" rather than simply swapping the dict values.\n",
" \"\"\"\n",
" new_op_x = Operation(ops[op_y].left_wire, ops[op_y].right_wire, ops[op_y].gate, op_x)\n",
" new_op_y = Operation(ops[op_x].left_wire, ops[op_x].right_wire, ops[op_x].gate, op_y)\n",
" \n",
" ops[op_x] = new_op_x\n",
" ops[op_y] = new_op_y"
" ops[op_x].output_wire = op_y\n",
" ops[op_y].output_wire = op_x\n",
" ops[op_x], ops[op_y] = ops[op_y], ops[op_x]"
]
},
{
Expand All @@ -9651,7 +9647,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -9757,10 +9753,6 @@
" \n",
" return True\n",
"\n",
"# def swap_ops(ops: dict, op_x: str, op_y: str):\n",
"# \"\"\" Swap two operations in the operations dictionary \"\"\"\n",
"# ops[op_x], ops[op_y] = ops[op_y], ops[op_x]\n",
"\n",
"def get_lowest_failure(operations, x_wires_count, start=2) -> int:\n",
" \"\"\" Identify which bit positions fail the validation, \n",
" i.e. when the z value does not represent the sum of the x and y wires.\n",
Expand Down Expand Up @@ -9922,7 +9914,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -9999,7 +9991,7 @@
" return verify_sn(left, num, ops) and verify_carry(right, num, ops) or \\\n",
" verify_sn(right, num, ops) and verify_carry(left, num, ops)\n",
"\n",
"def find_lowest_fail(ops: dict, wires_count: int, start=2):\n",
"def find_lowest_fail(ops: dict, wires_count: int, start=0):\n",
" \"\"\" Find the lowest bit position that fails the verification. \n",
" Returns -1 if all are valid. \"\"\"\n",
" for i in range(start, wires_count):\n",
Expand Down Expand Up @@ -10074,12 +10066,12 @@
"\n",
"Furthermore, with this rule set, we know what values are _expected_ from each rule, and we can compare against the operations we've actually got. Whenever we fail a verification, we can just substitute the value that is expected. So we take the old output wire and the expected output wire, and add them to our list of swaps.\n",
"\n",
"This is super fast!"
"This is super fast - runs in under 10ms."
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -10105,11 +10097,9 @@
" sn = xn ^ yn # An XOR of the input values themselves\n",
" \n",
" Returns None if the verification passes, otherwise a tuple of the two wires to swap.\n",
" \"\"\" \n",
" \"\"\"\n",
" assert num >= 2, \"Verification starts at z02, since z00 and z01 are special cases\"\n",
" wire = get_wire_name(\"z\", num)\n",
" if num == 0: # Special case for z00\n",
" if sorted([ops[wire].left_wire, ops[wire].right_wire]) == [\"x00\", \"y00\"]:\n",
" return None\n",
" \n",
" sn_prev = match_operation(ops, gate=\"XOR\", left=get_wire_name(\"x\", num-1), right=get_wire_name(\"y\", num-1))\n",
" ic_prev = match_operation(ops, gate=\"AND\", left=sn_prev.output_wire)\n",
Expand All @@ -10120,15 +10110,12 @@
" \n",
" # Failure scenarios - we need a swap\n",
" if zn is None or zn.output_wire != wire:\n",
" if zn is None:\n",
" # The rule is incorrect for this z wire\n",
" if zn is None: # The rule is incorrect for this z wire\n",
" zn = ops[wire] # Get the incorrect operation\n",
" # Find the difference between the two sets of wires\n",
" # I.e. the wires that currently connect to the output wire, and the wires that should\n",
" # Find the difference between the actual wires and the expected wires\n",
" # E.g. {\"ntr, \"bpt\"} ^ {\"ntr\", \"krj\"} = {\"bpt\", \"krj\"}\n",
" to_swap = set([zn.left_wire, zn.right_wire]) ^ set([sn.output_wire, cn.output_wire])\n",
" if zn.output_wire != wire:\n",
" # The rule is correct but the output wire is wrong\n",
" if zn.output_wire != wire: # The rule is correct but the output wire is wrong\n",
" to_swap = [wire, zn.output_wire] # E.g. wire should be \"z06\" but output_wire is \"fkp\"\n",
" \n",
" return sorted(to_swap)\n",
Expand Down

0 comments on commit 526decb

Please sign in to comment.