Skip to content

Instantly share code, notes, and snippets.

@sanity

sanity/99d.kt Secret

Created August 25, 2016 12:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanity/a81c697630cfc40d756d6d8068f3f49f to your computer and use it in GitHub Desktop.
Save sanity/a81c697630cfc40d756d6d8068f3f49f to your computer and use it in GitHub Desktop.
package nndvoteexp
import com.google.common.collect.Collections2
import java.util.*
/**
* Created by ian on 8/25/16.
*/
data class Vote(val design: Int, val voter: String, val vote: Int)
data class GreaterThan(val lesserDesign: Int, val greaterDesign: Int)
fun main(args: Array<String>) {
val votes = arrayListOf(
Vote(120, "test", 5),
Vote(120, "Anonymous", 2),
Vote(120, "victor", 4),
Vote(120, "michael", 5),
Vote(120, "charles", 5),
Vote(120, "juiceman", 2),
Vote(120, "fp", 5),
Vote(10, "test", 1),
Vote(10, "Anonymous", 1),
Vote(10, "victor", 5),
Vote(10, "michael", 2),
Vote(10, "charles", 3),
Vote(10, "juiceman", 4),
Vote(10, "fp", 2),
Vote(41, "test", 1),
Vote(41, "victor", 4),
Vote(41, "michael", 4),
Vote(41, "charles", 2),
Vote(41, "juiceman", 5),
Vote(41, "fp", 4),
Vote(114, "test", 1),
Vote(114, "Anonymous", 4),
Vote(114, "victor", 3),
Vote(114, "michael", 3),
Vote(114, "charles", 3),
Vote(114, "juiceman", 3),
Vote(114, "fp", 3)
)
val greaterThan = ArrayList<GreaterThan>()
for ((voter, votes) in votes.asSequence().groupBy { it.voter }) {
val sortedVotes = votes.sortedBy { it.vote }
val prev = HashSet<Vote>()
for (currentVote in sortedVotes) {
for (pv in prev) {
if (pv.vote < currentVote.vote)
greaterThan.add(GreaterThan(pv.design, currentVote.design))
}
prev.add(currentVote)
}
}
val designOrders = Collections2.permutations(votes.map { it.design }.distinct())
for (order in designOrders) {
var violations = 0
var prevDesign: Int? = null
for (design in order) {
prevDesign.let {
if (it != null) {
val wouldBeViolation = GreaterThan(design, it)
violations += greaterThan.count { it == wouldBeViolation }
}
}
prevDesign = design
}
println("$order\t$violations")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment