Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day289 #807

Merged
merged 2 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ problems from
* [Day 286](https://github.com/vaskoz/dailycodingproblem-go/issues/585)
* [Day 287](https://github.com/vaskoz/dailycodingproblem-go/issues/587)
* [Day 288](https://github.com/vaskoz/dailycodingproblem-go/issues/590)
* [Day 289](https://github.com/vaskoz/dailycodingproblem-go/issues/593)
* [Day 290](https://github.com/vaskoz/dailycodingproblem-go/issues/594)
* [Day 291](https://github.com/vaskoz/dailycodingproblem-go/issues/596)
* [Day 292](https://github.com/vaskoz/dailycodingproblem-go/issues/598)
Expand Down
13 changes: 13 additions & 0 deletions day289/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package day289

// CanFirstPlayerForceWinNimGame answers if the first player
// can force a win with the given heap configuration.
func CanFirstPlayerForceWinNimGame(heaps []int) bool {
sum := 0

for _, heap := range heaps {
sum ^= heap
}

return sum != 0
}
35 changes: 35 additions & 0 deletions day289/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day289

import "testing"

// nolint
var testcases = []struct {
heaps []int
forcedWin bool
}{
{[]int{3, 4, 5}, true},
{[]int{1, 2, 3}, false},
{[]int{1, 4, 5}, false},
{[]int{1, 6, 7}, false},
{[]int{5, 9, 12}, false},
{[]int{1, 2, 4, 7}, false},
{[]int{1, 2, 4, 6}, true},
}

func TestCanFirstPlayerForceWinNimGame(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if res := CanFirstPlayerForceWinNimGame(tc.heaps); res != tc.forcedWin {
t.Errorf("Expected %v, got %v", tc.forcedWin, res)
}
}
}

func BenchmarkCanFirstPlayerForceWinNimGame(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
CanFirstPlayerForceWinNimGame(tc.heaps)
}
}
}