Skip to content

Commit 8a5fb37

Browse files
committed
Day 24, part two: Label the graph's edges with the wire names
1 parent b84e493 commit 8a5fb37

File tree

3 files changed

+2132
-1844
lines changed

3 files changed

+2132
-1844
lines changed

src/main/kotlin/de/ronny_h/aoc/year24/day24/PlotCrossedWires.ipynb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
{
1818
"metadata": {
1919
"ExecuteTime": {
20-
"end_time": "2025-06-19T15:50:24.279065300Z",
21-
"start_time": "2025-06-19T15:50:24.119250200Z"
20+
"end_time": "2025-06-19T17:22:44.247196600Z",
21+
"start_time": "2025-06-19T17:22:44.089978900Z"
2222
}
2323
},
2424
"cell_type": "code",
@@ -42,8 +42,8 @@
4242
{
4343
"metadata": {
4444
"ExecuteTime": {
45-
"end_time": "2025-06-19T15:50:25.272229100Z",
46-
"start_time": "2025-06-19T15:50:24.295155300Z"
45+
"end_time": "2025-06-19T17:22:45.214580900Z",
46+
"start_time": "2025-06-19T17:22:44.256858700Z"
4747
}
4848
},
4949
"cell_type": "code",
@@ -53,6 +53,8 @@
5353
"\n",
5454
"val gatesWithId = gates.mapIndexed { i, g -> GateWithId(\"$i\", g.in1, g.in2, g.operation.toString(), g.out) }\n",
5555
"val wiresWithId = wires.mapIndexed { i, w -> WireWithId(\"${i + gates.size}\", w.name) }\n",
56+
"val gatesById = gatesWithId.associateBy { it.id }\n",
57+
"val wiresById = wiresWithId.associateBy { it.id }\n",
5658
"val gatesOutLookup = gatesWithId.associateBy { it.out }\n",
5759
"val gatesInLookup = gatesWithId.fold(emptyMap<String, GateWithId>()) { acc, gate ->\n",
5860
" (acc + (gate.in1 to gate)) + (gate.in2 to gate)\n",
@@ -74,8 +76,8 @@
7476
{
7577
"metadata": {
7678
"ExecuteTime": {
77-
"end_time": "2025-06-19T15:50:25.779627800Z",
78-
"start_time": "2025-06-19T15:50:25.282871300Z"
79+
"end_time": "2025-06-19T17:22:45.565090Z",
80+
"start_time": "2025-06-19T17:22:45.223580300Z"
7981
}
8082
},
8183
"cell_type": "code",
@@ -126,8 +128,8 @@
126128
{
127129
"metadata": {
128130
"ExecuteTime": {
129-
"end_time": "2025-06-19T15:50:26.593631900Z",
130-
"start_time": "2025-06-19T15:50:25.793419900Z"
131+
"end_time": "2025-06-19T17:22:46.433761400Z",
132+
"start_time": "2025-06-19T17:22:45.580090500Z"
131133
}
132134
},
133135
"cell_type": "code",
@@ -136,7 +138,7 @@
136138
"\n",
137139
"File(\"wiredGates.dot\").printWriter().use { out ->\n",
138140
" out.println(\"digraph {\")\n",
139-
" out.println(\" edge [arrowhead=vee arrowsize=0.6]\")\n",
141+
" out.println(\" edge [fontname=Arial fontsize=9 arrowhead=vee arrowsize=0.6]\")\n",
140142
" out.println(\" node [fontname=Arial shape=plain]\")\n",
141143
" val gatesCount = gatesWithId.size\n",
142144
" val outWires = gatesWithId.filter { gatesInLookup[it.out] == null }.map { it.out }\n",
@@ -162,7 +164,23 @@
162164
"\n",
163165
" wiredGateIds.first.forEachIndexed { i, fromId ->\n",
164166
" val toId = wiredGateIds.second[i]\n",
165-
" out.println(\" $fromId -> $toId\")\n",
167+
" val fromGate = gatesById[fromId]\n",
168+
" val attributes = if (fromGate != null) {\n",
169+
" if (fromGate.out.startsWith(\"z\")) {\n",
170+
" \" [color=$outZColor]\"\n",
171+
" } else {\n",
172+
" // the \"from\" and \"to\" nodes are gates -> label their connection\n",
173+
" \" [label=${fromGate.out}]\"\n",
174+
" }\n",
175+
" } else {\n",
176+
" // the \"from\" node is an input\n",
177+
" if (wiresById.getValue(fromId).name.startsWith(\"x\")) {\n",
178+
" \" [color=$inXColor]\"\n",
179+
" } else {\n",
180+
" \" [color=$inYColor]\"\n",
181+
" }\n",
182+
" }\n",
183+
" out.println(\" $fromId -> $toId$attributes\")\n",
166184
" }\n",
167185
"\n",
168186
" out.println(\"}\")\n",
@@ -175,7 +193,13 @@
175193
"metadata": {},
176194
"cell_type": "markdown",
177195
"source": [
178-
"The resulting graph looks like this:\n",
196+
"The graph can be generated by calling\n",
197+
"\n",
198+
"```bash\n",
199+
"dot wiredGates.dot -Tsvg -o wiredGates.svg\n",
200+
"```\n",
201+
"\n",
202+
"It looks like this:\n",
179203
"\n",
180204
"![A Graph showing the wired gates with inputs and outputs](./wiredGates.svg)"
181205
]

0 commit comments

Comments
 (0)