Skip to content

Commit

Permalink
day 303: clock hand angles
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jun 22, 2019
1 parent 1738605 commit 3584687
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 14 additions & 0 deletions day303/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ func AngleClockHands(hh, mm int) int {
}
return int(math.Round(math.Abs(diffAngle)))
}

// TimesOverlappingHands returns all the [hh:mm] pairs with overlapping
// clock hands by checking all possible 12*60 hour/minute possibilities.
func TimesOverlappingHands() [][]int {
var result [][]int
for h := 0; h < 12; h++ {
for m := 0; m < 60; m++ {
if angle := AngleClockHands(h, m); angle == 0 {
result = append(result, []int{h, m})
}
}
}
return result
}
22 changes: 21 additions & 1 deletion day303/problem_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package day303

import "testing"
import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
Expand Down Expand Up @@ -37,3 +40,20 @@ func BenchmarkAngleClockHands(b *testing.B) {
}
}
}

func TestTimesOverlappingHands(t *testing.T) {
t.Parallel()
result := TimesOverlappingHands()
expected := [][]int{
{0, 0}, {1, 5}, {2, 10}, {3, 15}, {4, 20}, {5, 25}, {6, 30}, {7, 35}, {8, 40}, {9, 45}, {10, 50}, {11, 55},
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, got %v", result, expected)
}
}

func BenchmarkTimesOverlappingHands(b *testing.B) {
for i := 0; i < b.N; i++ {
TimesOverlappingHands()
}
}

0 comments on commit 3584687

Please sign in to comment.