# HG changeset patch # User Sean E. Russell # Date 1615298951 21600 # Tue Mar 09 08:09:11 2021 -0600 # Node ID 92c42130af9d2ea54b680cb2b8afd223959be53a # Parent 77d3c65bb33fb7599b7aee950be14d6af3f5b205 Fixes build; prettifies output diff --git a/.build.yml b/.build.yml --- a/.build.yml +++ b/.build.yml @@ -6,7 +6,7 @@ - zip - curl sources: - - hg+https://hg.sr.ht/~ser/score-better-balance + - hg+https://hg.sr.ht/~ser/MARS secrets: - 45926939-38b0-4e7f-bfa5-1a5b5786a773 - d7d86c19-5b94-4c65-bf07-b8b30f55bd63 diff --git a/main.go b/main.go --- a/main.go +++ b/main.go @@ -54,7 +54,9 @@ if cycle { printScoreDetails(w, lines, badVotes, ints, diffs, ttl) // If there's a tie, then it's a hard tie - checkTie(lines[0], subtotal(ints), w) + if checkTie(lines[0], subtotal(ints), w) { + fmt.Println("Hard tie (no winner possible)") + } return } @@ -103,6 +105,22 @@ return ttls } +// subtotal sums up the columns, and returns a single row of the sums +func countVoters(lines [][]int) []int { + if len(lines) < 1 { + return []int{} + } + ttls := make([]int, len(lines[0])) + for _, row := range lines { + for c, cell := range row { + if cell != 0 { + ttls[c]++ + } + } + } + return ttls +} + // winner finds the column with the highest sum of values in a matrix, // and returns the index of the winning column as well as the calculated // score. the winner by score. @@ -231,6 +249,8 @@ printWithCandidates("Summary of distributions", true, header, uniq(lines)) subttl := subtotal(ints) printWithCandidates("Subtotals", false, header, subttl) + voters := countVoters(ints) + printWithCandidates("# voters", false, []string{}, voters) subWc := max(subttl) fmt.Printf("Winner by score: %s with %d\n", header[subWc], subttl[subWc]) } @@ -238,8 +258,8 @@ func printAdjustDetails(wc int, hlines, badVotes [][]string, ints [][]int, diffs [][]int, ttl []int) { header := hlines[0] printWithCandidates("Summary of diffs", true, header, uniq(toString(diffs))) - printWithCandidates("Modifiers", false, header, subtotal(diffs)) - printWithCandidates("Totals", false, header, ttl) + printWithCandidates("Modifiers", false, []string{}, subtotal(diffs)) + printWithCandidates("Totals", false, []string{}, ttl) fmt.Printf("Highest combined total: %s with %d\n", header[wc], ttl[wc]) if len(badVotes) > 0 { @@ -314,6 +334,9 @@ // the same way. func printWithCandidates(d string, count bool, cands []string, v interface{}) { fmt.Println(d) + if len(cands) > 0 { + fmt.Printf(" \t%s\n", strings.Join(cands, "\t")) + } switch a := v.(type) { case [][]string: if count { @@ -321,7 +344,6 @@ return a[i][0] > a[j][0] }) } - fmt.Printf(" \t%s\n", strings.Join(cands, "\t")) for _, r := range a { if !count { fmt.Printf(" \t") @@ -334,7 +356,6 @@ return a[i][0] > a[j][0] }) } - fmt.Printf(" \t%s\n", strings.Join(cands, "\t")) for _, r := range a { if !count { fmt.Printf(" \t") @@ -350,7 +371,6 @@ return a[i] > a[j] }) } - fmt.Printf(" \t%s\n", strings.Join(cands, "\t")) if !count { fmt.Printf(" \t") }