Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 289
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

The game of Nim is played as follows. Starting with three heaps, each containing a variable number of items, two players take turns removing one or more items from a single pile. The player who eventually is forced to take the last stone loses. For example, if the initial heap sizes are 3, 4, and 5, a game could be played as shown below:
```
  A  |  B  |  C
-----------------
  3  |  4  |  5
  3  |  1  |  3
  3  |  1  |  3
  0  |  1  |  3
  0  |  1  |  0
  0  |  0  |  0 
```
In other words, to start, the first player takes three items from pile B. The second player responds by removing two stones from pile C. The game continues in this way until player one takes last stone and loses.

Given a list of non-zero starting values `[a, b, c]`, and assuming optimal play, determine whether the first player has a forced win.

Pull request description:
Day289
Resolves #593.

Patch:
diff --git a/README.md b/README.md
index fe4c781..c05c33b 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/day289/problem.go b/day289/problem.go
new file mode 100644
index 0000000..f18c801
--- /dev/null
+++ b/day289/problem.go
@@ -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
+}


Test patch:
diff --git a/day289/problem_test.go b/day289/problem_test.go
new file mode 100644
index 0000000..b57990a
--- /dev/null
+++ b/day289/problem_test.go
@@ -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)
+		}
+	}
+}