-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use jgrapht to solve final day. Wrote a graphviz output and could see the output, but jgrapht actually solves it using Stoer-Wagner Minimum Cut!
- Loading branch information
1 parent
96fc1a7
commit 4f62ce8
Showing
6 changed files
with
1,430 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package net.fish.y2023 | ||
|
||
import net.fish.Day | ||
import net.fish.resourceLines | ||
import org.jgrapht.alg.StoerWagnerMinimumCut | ||
import org.jgrapht.graph.DefaultWeightedEdge | ||
import org.jgrapht.graph.SimpleWeightedGraph | ||
|
||
object Day25 : Day { | ||
private val data by lazy { resourceLines(2023, 25) } | ||
|
||
override fun part1() = doPart1(data) | ||
override fun part2() = doPart2(data) | ||
|
||
fun doPart1(data: List<String>): Int { | ||
val graph = SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge::class.java) | ||
data.forEach { line -> | ||
val (name, others) = line.split(": ") | ||
graph.addVertex(name) | ||
others.split(" ").forEach { other -> | ||
graph.addVertex(other) | ||
graph.addEdge(name, other) | ||
} | ||
} | ||
|
||
val oneSide = StoerWagnerMinimumCut(graph).minCut() | ||
return (graph.vertexSet().size - oneSide.size) * oneSide.size | ||
} | ||
|
||
fun doPart2(data: List<String>): Int = data.size | ||
|
||
// clearly 3 lines separate the 2 blobs. printing as SVG was possible to find the nodes | ||
fun toGraphViz(data: List<String>): String { | ||
val builder = StringBuilder() | ||
builder.append("digraph Day25 {\n") | ||
for (line in data) { | ||
val parts = line.split(":") | ||
val node = parts[0].trim() | ||
val edges = parts[1].split(" ") | ||
for (edge in edges) { | ||
if (edge.trim() != "") { | ||
builder.append(" $node -> ${edge.trim()};\n") | ||
} | ||
} | ||
} | ||
builder.append("}\n") | ||
return builder.toString() | ||
} | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
println(part1()) | ||
// println(part2()) | ||
} | ||
|
||
} |
Oops, something went wrong.