From c9cb1df11e4796946b279095f76af85105a80a40 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Thu, 6 Dec 2018 11:02:22 -0700 Subject: [PATCH 1/2] day 106: specific number of hops to reach the end --- day106/problem.go | 13 +++++++++++++ day106/problem_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 day106/problem.go create mode 100644 day106/problem_test.go diff --git a/day106/problem.go b/day106/problem.go new file mode 100644 index 0000000..4f35dbe --- /dev/null +++ b/day106/problem.go @@ -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 +} diff --git a/day106/problem_test.go b/day106/problem_test.go new file mode 100644 index 0000000..cbed1fc --- /dev/null +++ b/day106/problem_test.go @@ -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) + } + } +} From 41e50dc51281fbe4c5c7da0fd690b622caae57bf Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Thu, 6 Dec 2018 11:02:49 -0700 Subject: [PATCH 2/2] add day 106 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e1ff522..24deacd 100644 --- a/README.md +++ b/README.md @@ -115,3 +115,4 @@ problems from * [Day 103](https://github.com/vaskoz/dailycodingproblem-go/issues/217) * [Day 104](https://github.com/vaskoz/dailycodingproblem-go/issues/219) * [Day 105](https://github.com/vaskoz/dailycodingproblem-go/issues/221) +* [Day 106](https://github.com/vaskoz/dailycodingproblem-go/issues/223)