-
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.
Merge pull request #224 from vaskoz/day106
Day106
- Loading branch information
Showing
3 changed files
with
42 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,13 @@ | ||
package day106 | ||
|
||
// CanHopToEnd can you hop from 0 to the end. | ||
func CanHopToEnd(hops []int) bool { | ||
start, end := 0, len(hops)-1 | ||
for start < end { | ||
if hops[start] == 0 { | ||
break | ||
} | ||
start += hops[start] | ||
} | ||
return start == end | ||
} |
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,28 @@ | ||
package day106 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
hops []int | ||
expected bool | ||
}{ | ||
{[]int{2, 0, 1, 0}, true}, | ||
{[]int{1, 1, 0, 1}, false}, | ||
} | ||
|
||
func TestCanHopToEnd(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := CanHopToEnd(tc.hops); result != tc.expected { | ||
t.Errorf("For %v expected %v got %v", tc.hops, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkCanHopToEnd(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
CanHopToEnd(tc.hops) | ||
} | ||
} | ||
} |