aoc2022/d16/draw.ipynb

107 lines
17 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 39,
"id": "6b0b44f6-bcbf-4092-9201-d1049430e165",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"sample = \"\"\"\\\n",
"Valve AA has flow rate=0; tunnels lead to valves DD, II, BB\n",
"Valve BB has flow rate=13; tunnels lead to valves CC, AA\n",
"Valve CC has flow rate=2; tunnels lead to valves DD, BB\n",
"Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE\n",
"Valve EE has flow rate=3; tunnels lead to valves FF, DD\n",
"Valve FF has flow rate=0; tunnels lead to valves EE, GG\n",
"Valve GG has flow rate=0; tunnels lead to valves FF, HH\n",
"Valve HH has flow rate=22; tunnel leads to valve GG\n",
"Valve II has flow rate=0; tunnels lead to valves AA, JJ\n",
"Valve JJ has flow rate=21; tunnel leads to valve II\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "3c34673d-f03a-43ee-89b7-0ee8e953c827",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import re\n",
"pat = re.compile('Valve ([A-Z]{2}) has flow rate=([0-9]+); tunnels? leads? to valves? ([ ,A-Z]+)')\n",
"P = dict()\n",
"for i,l in enumerate(sample.splitlines()):\n",
" g = pat.search(l)\n",
" P[g[1]] = (i, int(g[2]), g[3].split(\", \"))\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "8c46fa33-d751-444a-9a41-1bb2145df583",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import networkx as nx\n",
"G = nx.DiGraph()\n",
"for (k,(i,o,V)) in P.items():\n",
" for v in V:\n",
" G.add_edge(k,v) #,label=str(i))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "82fdac2e-de6d-44bf-b117-a00addbd8d49",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWH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tI3xQphPALsjFlM2XlHGDq0jwsdgcN/R1l+TWAl+cW8e3eEiZlpjA6Pcm7RQohAoKE7nnUBu5uqm3qea91OqHa5mDq0t0AErxCiLPIF2mN2HaojKlL8y4ocE9XbVOZujSP7QVlnilMCBGwJHQbMWN1Pha7A4DCeRM4NH0ETrvtrOtsZYUcnHYnpctmuI5Z7A5mrs73Wq1CiMAgoXsOJWYra/YW43SCvawIa0EuKApV+evPurZy5zdojGFU7f7OFcpOJ6zaU0yp2ert0oUQfkxC9xyyNxe4/t288xsM8Z0J7T6Ayh1f17vO6XRSufMbIm+8DzS6eqGsANlbChBCiDoSuueQV1jumhZWufMbQrtmENq1P9X7t+CoPOG6zlqwC3tFCSGpNxKacn29ULbYVfKOVni9diGE/5LQPYdyix0Ay6Fd2MuPEZJyPYbYZHSRcVTuWuO6rnLHNwR3uBqtMYzQrv1+DeWy0+5z9hiwEKLlktA9B5OxdjZd5c6vCW5/FdqQCABCU/th3ln7NKvarFTuWUto1wwADAld0JliqMxdc9p99N4tXAjh12SeLmCxWNBqtej1pwKyc9tw9E4blXlrQVU59O/RtSfsNlRrJTVF+7CV/ILTWsXxZTM5vmIWAKqlEvOOrzH1vgujTkNKXLgv3pIQwk9J6AI33XQTP/zwAyEhIYSGhlJVVYUmJILga3+LomiIe+gtFO2pQC7+dBrmnd9gKzlIaNp
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"nx.draw_networkx(G, with_labels=True)\n",
"plt.draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}