-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package day300 | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"sort" | ||
) | ||
|
||
var ( | ||
out io.Writer = os.Stdout // nolint | ||
in io.Reader = os.Stdin // nolint | ||
) | ||
|
||
// VotingResult is the number of votes a candidate received. | ||
type VotingResult struct { | ||
Candidate, Votes int | ||
} | ||
|
||
func main() { | ||
var voterID, candidateID int | ||
voters := make(map[int]struct{}) | ||
candidates := make(map[int]*VotingResult) | ||
top3 := make([]*VotingResult, 0, 3) | ||
_, err := fmt.Fscanf(in, "%d,%d", &voterID, &candidateID) | ||
for err != io.EOF { | ||
if _, voted := voters[voterID]; voted { | ||
fmt.Fprintf(out, "voter %d already voted. this vote for candidate %d will not count\n", voterID, candidateID) | ||
} else { | ||
voters[voterID] = struct{}{} | ||
if cand, exists := candidates[candidateID]; !exists { | ||
candidates[candidateID] = &VotingResult{Candidate: candidateID, Votes: 1} | ||
if len(top3) < 3 { | ||
top3 = append(top3, candidates[candidateID]) | ||
} | ||
} else { | ||
cand.Votes++ | ||
if cand.Votes > top3[0].Votes && !Contains(top3, candidates[candidateID]) { | ||
top3[0] = cand | ||
} | ||
} | ||
sort.Slice(top3, func(i, j int) bool { | ||
return top3[i].Votes < top3[j].Votes | ||
}) | ||
for _, v := range top3 { | ||
fmt.Fprintf(out, "%d,%v ", v.Candidate, v.Votes) | ||
} | ||
fmt.Fprintln(out) | ||
} | ||
_, err = fmt.Fscanf(in, "%d,%d", &voterID, &candidateID) | ||
} | ||
} | ||
|
||
func Contains(top3 []*VotingResult, cand *VotingResult) bool { | ||
for _, c := range top3 { | ||
if cand == c { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,42 @@ | ||
package day300 | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input, output string | ||
}{ | ||
{"1,1 2,2 3,3 4,3 5,3", "1,1 \n1,1 2,1 \n1,1 2,1 3,1 \n1,1 2,1 3,2 \n1,1 2,1 3,3 \n"}, | ||
{"1,1 2,2 3,3 1,3", "1,1 \n1,1 2,1 \n1,1 2,1 3,1 \nvoter 1 already voted. this vote for candidate 3 will not count\n"}, | ||
{"1,1 2,2 3,3 4,4 5,4", | ||
"1,1 \n1,1 2,1 \n1,1 2,1 3,1 \n1,1 2,1 3,1 \n2,1 3,1 4,2 \n"}, | ||
} | ||
|
||
func TestMainVoterTop3(t *testing.T) { | ||
// don't run in parallel due to global var | ||
for _, tc := range testcases { | ||
buff := new(bytes.Buffer) | ||
out = buff | ||
in = strings.NewReader(tc.input) | ||
main() | ||
if result := buff.String(); result != tc.output { | ||
t.Errorf("Expected \n%s, got \n%s", tc.output, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMainVoterTop3(b *testing.B) { | ||
// don't run in parallel due to global var | ||
buff := new(bytes.Buffer) | ||
out = buff | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
in = strings.NewReader(tc.input) | ||
main() | ||
} | ||
} | ||
} |