Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day292 #625

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ problems from
* [Day 288](https://github.com/vaskoz/dailycodingproblem-go/issues/590)
* [Day 290](https://github.com/vaskoz/dailycodingproblem-go/issues/594)
* [Day 291](https://github.com/vaskoz/dailycodingproblem-go/issues/596)
* [Day 292](https://github.com/vaskoz/dailycodingproblem-go/issues/598)
* [Day 294](https://github.com/vaskoz/dailycodingproblem-go/issues/600)
* [Day 295](https://github.com/vaskoz/dailycodingproblem-go/issues/601)
* [Day 296](https://github.com/vaskoz/dailycodingproblem-go/issues/605)
Expand Down
68 changes: 68 additions & 0 deletions day292/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package day292

import (
"errors"
"sort"
)

var errTeamNotPossible = errors.New("building a team without enemies is impossible")

// ErrTeamNotPossible returns the error when a team isn't possible.
func ErrTeamNotPossible() error {
return errTeamNotPossible
}

// AssignTeams attempts to assign teammates using a constraint of enemies.
// It uses Breadth-First-Search to accomplish this.
func AssignTeams(enemies map[int]map[int]struct{}) ([]int, []int, error) {
team1 := make(map[int]bool)
team2 := make(map[int]bool)
var q []int
for id := range enemies {
team1[id] = true
for e := range enemies[id] {
q = append(q, e)
}
break
}
for len(q) != 0 {
var nextQ []int
var countNew int
for _, id := range q {
if _, found := team1[id]; found {
return nil, nil, errTeamNotPossible
}
if _, found := team2[id]; !found {
team2[id] = true
countNew++
for enemy := range enemies[id] {
nextQ = append(nextQ, enemy)
}
}
}
if countNew > 0 {
q = nextQ
} else if countNew == 0 {
q = nil
}
team1, team2 = team2, team1
}
return buildResult(team1, team2)
}

func buildResult(team1, team2 map[int]bool) ([]int, []int, error) {
result1 := make([]int, 0, len(team1))
for id := range team1 {
result1 = append(result1, id)
}
result2 := make([]int, 0, len(team2))
for id := range team2 {
result2 = append(result2, id)
}
sort.Slice(result1, func(i, j int) bool { return result1[i] < result1[j] })
sort.Slice(result2, func(i, j int) bool { return result2[i] < result2[j] })
if result1[0] < result2[0] {
return result1, result2, nil
}
return result2, result1, nil
}
94 changes: 94 additions & 0 deletions day292/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package day292

import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
enemies map[int]map[int]struct{}
teamOne, teamTwo []int
err error
}{
{
map[int]map[int]struct{}{
0: {3: struct{}{}},
1: {2: struct{}{}},
2: {
1: struct{}{},
4: struct{}{},
},
3: {
0: struct{}{},
4: struct{}{},
5: struct{}{},
},
4: {
2: struct{}{},
3: struct{}{},
},
5: {3: struct{}{}},
},
[]int{0, 1, 4, 5},
[]int{2, 3},
nil,
},
{
map[int]map[int]struct{}{
0: {3: struct{}{}},
1: {2: struct{}{}},
2: {
1: struct{}{},
4: struct{}{},
},
3: {
0: struct{}{},
2: struct{}{},
4: struct{}{},
5: struct{}{},
},
4: {
2: struct{}{},
3: struct{}{},
},
5: {3: struct{}{}},
},
nil,
nil,
ErrTeamNotPossible(),
},
{
map[int]map[int]struct{}{
1: {0: struct{}{}},
0: {
1: struct{}{},
2: struct{}{},
3: struct{}{},
},
2: {0: struct{}{}},
3: {0: struct{}{}},
},
[]int{0},
[]int{1, 2, 3},
nil,
},
}

func TestAssignTeams(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if t1, t2, err := AssignTeams(tc.enemies); !reflect.DeepEqual(t1, tc.teamOne) ||
!reflect.DeepEqual(t2, tc.teamTwo) || err != tc.err {
t.Errorf("Expected (%v,%v,%v), got (%v,%v,%v)", tc.teamOne, tc.teamTwo, tc.err, t1, t2, err)
}
}
}

func BenchmarkAssignTeams(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
AssignTeams(tc.enemies) // nolint
}
}
}