Created
August 19, 2015 22:28
-
-
Save edmundnoble/b4ccd4b72a77fe9ff7e8 to your computer and use it in GitHub Desktop.
DA: Inquiisition constellation puzzle solver
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
// Astrarium constellation puzzle solver for DA: Inquisition. Just assign numeric labels to the stars, and record | |
// the connections between them in the "edges" container. | |
object ConstellationSolver extends App { | |
type Edge = (Int, Int) | |
def E(i : Int, f : Int) = (i, f) | |
def orderEdges(edges : Set[Edge]) : Set[Seq[Edge]] = { | |
def rec(remaining : Set[Edge], sofar : Seq[Edge]) : Set[Seq[Edge]] = { | |
if (remaining.isEmpty) { | |
Set(sofar) | |
} else { | |
val connectedEdges = remaining.filter { | |
case edge@(first, second) ⇒ connected(sofar.last, edge) | |
} | |
connectedEdges.flatMap { | |
case edge@(first, second) ⇒ rec(remaining - edge, sofar :+ edge) | |
} | |
} | |
} | |
edges.flatMap(edge ⇒ rec(edges - edge, Seq(edge))) | |
} | |
def connected(edge : Edge, other : Edge) = | |
edge._1 == other._1 || edge._2 == other._2 || edge._1 == other._2 || edge._2 == other._1 | |
val edges = Set( | |
E(1, 2), E(2, 3), E(3, 4), E(4, 5), E(5, 6), | |
E(6, 7), E(7, 8), E(8, 9), E(9, 10), E(10, 11), | |
E(11, 12), E(12, 13), E(13, 14), E(4, 15), E(4, 17), | |
E(15, 16), E(16, 11), E(17, 11) | |
) | |
val orders = orderEdges(edges) | |
println(orders.head) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment