Skip to content

Commit

Permalink
Merge pull request #603 from vaskoz/day280deterministic
Browse files Browse the repository at this point in the history
day 280: make it more deterministic
  • Loading branch information
vaskoz authored Jun 13, 2019
2 parents 709efe0 + b415447 commit 06c53ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
7 changes: 1 addition & 6 deletions day280/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ package day280
type UndirectedGraph map[int]map[int]struct{}

// HasCycle answers if the connected undirected graph is cyclic.
func HasCycle(g UndirectedGraph) bool {
var start int
for node := range g {
start = node
break
}
func HasCycle(g UndirectedGraph, start int) bool {
visits := make(map[int]bool)
return hasCycle(g, start, visits)
}
Expand Down
7 changes: 5 additions & 2 deletions day280/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "testing"
// nolint
var testcases = []struct {
g UndirectedGraph
start int
cyclic bool
}{
{
Expand All @@ -14,6 +15,7 @@ var testcases = []struct {
2: {0: struct{}{}},
3: {4: struct{}{}},
},
0,
true,
},
{
Expand All @@ -23,14 +25,15 @@ var testcases = []struct {
2: {3: struct{}{}},
3: {4: struct{}{}},
},
0,
false,
},
}

func TestHasCycle(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if cyclic := HasCycle(tc.g); cyclic != tc.cyclic {
if cyclic := HasCycle(tc.g, tc.start); cyclic != tc.cyclic {
t.Errorf("Expected %v, got %v", tc.cyclic, cyclic)
}
}
Expand All @@ -39,7 +42,7 @@ func TestHasCycle(t *testing.T) {
func BenchmarkHasCycle(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
HasCycle(tc.g)
HasCycle(tc.g, tc.start)
}
}
}

0 comments on commit 06c53ac

Please sign in to comment.