From c3821c9aeca085eb1b6fe8c4efa57ef688b5b1e3 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 7 Jun 2019 06:12:17 -0600 Subject: [PATCH 1/3] day 265: calculate bonuses based on LoC --- day265/problem.go | 53 ++++++++++++++++++++++++++++++++++++++++++ day265/problem_test.go | 35 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 day265/problem.go create mode 100644 day265/problem_test.go diff --git a/day265/problem.go b/day265/problem.go new file mode 100644 index 0000000..91ea8e0 --- /dev/null +++ b/day265/problem.go @@ -0,0 +1,53 @@ +package day265 + +// Bonuses determines how much bonus to give based on lines of code. +func Bonuses(loc []int) []int { + if len(loc) == 0 { + return nil + } else if len(loc) == 1 { + return []int{1} + } + bonuses := make([]int, 0, len(loc)) + segs := segments(loc) + for _, seg := range segs { + asc, delta := seg.ascending, seg.delta + segBonuses := make([]int, delta) + for i := range segBonuses { + segBonuses[i] = i + } + if !asc { + for i := 0; i < len(segBonuses)/2; i++ { + segBonuses[i], segBonuses[len(segBonuses)-1-i] = segBonuses[len(segBonuses)-1-i], segBonuses[i] + } + } + bonuses = append(bonuses, segBonuses...) + } + for i, x := range bonuses { + bonuses[i] = x + 1 + } + return bonuses +} + +type segment struct { + ascending bool + delta int +} + +func segments(loc []int) []segment { + asc := loc[1] > loc[0] + prev := loc[0] + var start int + var segs []segment + + slice := loc[1:] + for i, num := range slice { + if (asc && num < prev) || (!asc && num > prev) { + segs = append(segs, segment{asc, i - start + 1}) + start = i + 1 + asc = !asc + } + prev = num + } + segs = append(segs, segment{asc, len(loc) - start}) + return segs +} diff --git a/day265/problem_test.go b/day265/problem_test.go new file mode 100644 index 0000000..1bef339 --- /dev/null +++ b/day265/problem_test.go @@ -0,0 +1,35 @@ +package day265 + +import ( + "reflect" + "testing" +) + +// nolint +var testcases = []struct { + locs []int + bonuses []int +}{ + {[]int{10, 40, 200, 1000, 60, 30}, []int{1, 2, 3, 4, 2, 1}}, + {[]int{}, nil}, + {nil, nil}, + {[]int{1000}, []int{1}}, + {[]int{10, 40, 200, 1000, 900, 800, 30}, []int{1, 2, 3, 4, 3, 2, 1}}, +} + +func TestBonuses(t *testing.T) { + t.Parallel() + for _, tc := range testcases { + if bonuses := Bonuses(tc.locs); !reflect.DeepEqual(bonuses, tc.bonuses) { + t.Errorf("Expected %v, got %v", tc.bonuses, bonuses) + } + } +} + +func BenchmarkBonuses(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tc := range testcases { + Bonuses(tc.locs) + } + } +} From ae125992c106814952bfaec6a165377a9aa95dcf Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 7 Jun 2019 06:12:57 -0600 Subject: [PATCH 2/3] add day 265 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 462f3a6..f20815b 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ problems from * [Day 262](https://github.com/vaskoz/dailycodingproblem-go/issues/538) * [Day 263](https://github.com/vaskoz/dailycodingproblem-go/issues/539) * [Day 264](https://github.com/vaskoz/dailycodingproblem-go/issues/540) +* [Day 265](https://github.com/vaskoz/dailycodingproblem-go/issues/541) * [Day 266](https://github.com/vaskoz/dailycodingproblem-go/issues/542) * [Day 267](https://github.com/vaskoz/dailycodingproblem-go/issues/543) * [Day 268](https://github.com/vaskoz/dailycodingproblem-go/issues/544) From 2da1ccab39473fd706f4e253cd5828de9b02b39b Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 7 Jun 2019 09:38:14 -0600 Subject: [PATCH 3/3] day 265: variable rename --- day265/problem.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/day265/problem.go b/day265/problem.go index 91ea8e0..69e7a63 100644 --- a/day265/problem.go +++ b/day265/problem.go @@ -10,8 +10,8 @@ func Bonuses(loc []int) []int { bonuses := make([]int, 0, len(loc)) segs := segments(loc) for _, seg := range segs { - asc, delta := seg.ascending, seg.delta - segBonuses := make([]int, delta) + asc, run := seg.ascending, seg.run + segBonuses := make([]int, run) for i := range segBonuses { segBonuses[i] = i } @@ -30,7 +30,7 @@ func Bonuses(loc []int) []int { type segment struct { ascending bool - delta int + run int } func segments(loc []int) []segment {