Skip to content

Commit 530476a

Browse files
committed
doc tweaking
1 parent 61ebc64 commit 530476a

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 28,
67+
"execution_count": 41,
6868
"metadata": {
6969
"id": "p5Ki_HvOJUWk",
7070
"tags": []
@@ -258,7 +258,7 @@
258258
},
259259
{
260260
"cell_type": "code",
261-
"execution_count": 31,
261+
"execution_count": 44,
262262
"metadata": {
263263
"id": "lwP0r3BAaxjt",
264264
"tags": []
@@ -331,7 +331,7 @@
331331
},
332332
{
333333
"cell_type": "code",
334-
"execution_count": 32,
334+
"execution_count": 45,
335335
"metadata": {
336336
"id": "Y6nbd6WMryWi",
337337
"tags": []
@@ -359,7 +359,7 @@
359359
},
360360
{
361361
"cell_type": "code",
362-
"execution_count": 33,
362+
"execution_count": 46,
363363
"metadata": {
364364
"id": "A8sU4Ez_bBKl",
365365
"tags": []
@@ -529,7 +529,7 @@
529529
},
530530
{
531531
"cell_type": "code",
532-
"execution_count": 34,
532+
"execution_count": 47,
533533
"metadata": {
534534
"id": "DT5FSYliC9wp",
535535
"tags": []
@@ -8963,7 +8963,7 @@
89638963
},
89648964
{
89658965
"cell_type": "code",
8966-
"execution_count": 37,
8966+
"execution_count": 50,
89678967
"metadata": {},
89688968
"outputs": [],
89698969
"source": [
@@ -9147,7 +9147,7 @@
91479147
"\n",
91489148
"E.g. we might need to do swaps like this:\n",
91499149
"\n",
9150-
"<img src=\"https://aoc.just2good.co.uk/assets/images/gate-pairs.png\" width=\"480\" alt=\"Gate swaps\" />\n",
9150+
"<img src=\"https://aoc.just2good.co.uk/assets/images/gate-pairs.png\" width=\"420\" alt=\"Gate swaps\" />\n",
91519151
"\n",
91529152
"E.g. from:\n",
91539153
"\n",
@@ -9229,11 +9229,11 @@
92299229
"\n",
92309230
"A single adder can be depicted like this:\n",
92319231
"\n",
9232-
"<img src=\"https://aoc.just2good.co.uk/assets/images/1-bit-fa.png\" width=\"420\" alt=\"Full Adder\" />\n",
9232+
"<img src=\"https://aoc.just2good.co.uk/assets/images/1-bit-fa.png\" width=\"280px\" alt=\"Full Adder\" />\n",
92339233
"\n",
92349234
"So we can implement a multi-bit ripple adder like this:\n",
92359235
"\n",
9236-
"<img src=\"https://aoc.just2good.co.uk/assets/images/ripple-adder.png\" width=\"420\" alt=\"Ripple Adder\" />\n",
9236+
"<img src=\"https://aoc.just2good.co.uk/assets/images/ripple-adder.png\" width=\"480px\" alt=\"Ripple Adder\" />\n",
92379237
"\n",
92389238
"And this is how we can generate a single 5-bit number from the addition of two 4-bit numbers.\n",
92399239
"\n",
@@ -9250,42 +9250,42 @@
92509250
"source": [
92519251
"op_by_ouput = {}\n",
92529252
"\n",
9253-
"def get_code_for_output(wire: str) -> str:\n",
9253+
"def get_code_for_wire(wire: str) -> str:\n",
92549254
" \"\"\" Generate a string representation of the logical operations needed \n",
92559255
" to compute the value of a given wire. Uses recursion to handle dependencies between operations. \"\"\"\n",
92569256
" \n",
92579257
" # Base case - exit once we have a wire that requires no further op to calculate\n",
9258-
" if wire not in op_by_ouput:\n",
9258+
" if wire not in op_by_ouput: # I.e. this wire is not the output of an operation\n",
92599259
" return wire\n",
92609260
" \n",
9261+
" # Otherwise, get the operation required to output to this wire\n",
92619262
" left, right, gate = op_by_ouput[wire]\n",
92629263
" \n",
9263-
" # Recursive calls\n",
92649264
" # If the left or right inputs are themselves the result of other operations, \n",
92659265
" # these recursive calls will generate the code for those operations first.\n",
9266-
" left_code = get_code_for_output(left)\n",
9267-
" right_code = get_code_for_output(right)\n",
9268-
" \n",
9269-
" # Prevents (a & b) and (b & a) from being different strings.\n",
9270-
" left_code, right_code = sorted((left_code, right_code))\n",
9266+
" # Sort so that (a & b) and (b & a) are the same.\n",
9267+
" left_code, right_code = sorted((get_code_for_wire(left), get_code_for_wire(right)))\n",
92719268
" \n",
92729269
" match gate:\n",
9273-
" case \"AND\":\n",
9274-
" return f\"({left_code} & {right_code})\"\n",
9275-
" case \"OR\":\n",
9276-
" return f\"({left_code} | {right_code})\"\n",
9277-
" case \"XOR\":\n",
9278-
" return f\"({left_code} ^ {right_code})\"\n",
9270+
" case \"AND\": gate_code = \"&\"\n",
9271+
" case \"OR\": gate_code = \"|\"\n",
9272+
" case \"XOR\": gate_code = \"^\"\n",
9273+
"\n",
9274+
" return f\"({left_code} {gate_code} {right_code})\"\n",
92799275
" \n",
92809276
"def create_vis(wires: dict):\n",
9277+
" \"\"\" Creates a visual representation of a circuit using Graphviz. \"\"\"\n",
9278+
" \n",
92819279
" dot = graphviz.Digraph(format='png', engine='dot')\n",
92829280
" \n",
9283-
" # Define gate shapes, colors, and output styles\n",
9281+
" # Define gate shapes, colors, and output styles for different types of gate\n",
92849282
" gate_styles = {\n",
92859283
" \"AND\": {\"shape\": \"invtrapezium\", \"fillcolor\": \"lightblue\"},\n",
92869284
" \"OR\": {\"shape\": \"invtriangle\", \"fillcolor\": \"lightgreen\"},\n",
92879285
" \"XOR\": {\"shape\": \"box\", \"fillcolor\": \"lightyellow\"},\n",
92889286
" }\n",
9287+
" \n",
9288+
" # Define styles for output nodes based on their initial character\n",
92899289
" output_styles = {\n",
92909290
" \"z\": {\"fillcolor\": \"black\", \"fontcolor\": \"white\"},\n",
92919291
" \"x\": {\"fillcolor\": \"blue\", \"fontcolor\": \"white\"},\n",
@@ -9301,7 +9301,7 @@
93019301
" for output in op_by_ouput:\n",
93029302
" left, right, gate = op_by_ouput[output]\n",
93039303
"\n",
9304-
" # Create gate node in a box\n",
9304+
" # Create gate node\n",
93059305
" gate_label = f'{gate}'\n",
93069306
" gate_name = f'{left}_{right}_{gate}' # Unique gate name\n",
93079307
" gate_style = gate_styles.get(gate, {\"shape\": \"box\"})\n",
@@ -9317,12 +9317,12 @@
93179317
"\n",
93189318
" # Connect gate to output\n",
93199319
" output_style = output_styles.get(output[0], {}) \n",
9320-
" dot.node(output, output, shape='circle', style='filled', **output_style) # Output node as circle\n",
9320+
" dot.node(output, output, shape='circle', style='filled', **output_style)\n",
93219321
" dot.edge(gate_name, output)\n",
93229322
"\n",
93239323
" # Render the graph to a file\n",
93249324
" output_path = 'adder_operations'\n",
9325-
" dot.render(f\"{locations.output_dir}/{output_path}\") # Saves the output to 'circuit_graph.png'\n",
9325+
" dot.render(f\"{locations.output_dir}/{output_path}\") # Saves the output as a png\n",
93269326
" display(Image(f\"{locations.output_dir}/{output_path}.png\"))\n",
93279327
"\n",
93289328
"def visualise_operations(data: str):\n",
@@ -9331,10 +9331,11 @@
93319331
" for output in outputs:\n",
93329332
" op_by_ouput[output] = (left, right, gate)\n",
93339333
" \n",
9334+
" # View text-based representation of all wires\n",
93349335
" for output in sorted(op_by_ouput.keys()):\n",
9335-
" logger.info(f\"{output} = {get_code_for_output(output)}\")\n",
9336+
" print(f\"{output} = {get_code_for_wire(output)}\")\n",
93369337
" \n",
9337-
" create_vis(wires)\n",
9338+
" create_vis(wires) # Create a graphical representation\n",
93389339
" \n",
93399340
"soln = visualise_operations(input_data)"
93409341
]

0 commit comments

Comments
 (0)