Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 334
Good morning! Here's your coding interview problem for today.

This problem was asked by Twitter.

The 24 game is played as follows. You are given a list of four integers, each between 1 and 9, in a fixed order. By placing the operators `+, -, *, and /` between the numbers, and grouping them with parentheses, determine whether it is possible to reach the value 24.

For example, given the input `[5, 2, 7, 8]`, you should return `True`, since `(5 * 2 - 7) * 8 = 24`.

Write a function that plays the 24 game.

Pull request description:
Day334
Resolves #670.

Patch:
diff --git a/README.md b/README.md
index 96d4618..6d1d008 100644
--- a/README.md
+++ b/README.md
@@ -336,6 +336,7 @@ problems from
 * [Day 331](https://github.com/vaskoz/dailycodingproblem-go/issues/666)
 * [Day 332](https://github.com/vaskoz/dailycodingproblem-go/issues/669)
 * [Day 333](https://github.com/vaskoz/dailycodingproblem-go/issues/668)
+* [Day 334](https://github.com/vaskoz/dailycodingproblem-go/issues/670)
 * [Day 337](https://github.com/vaskoz/dailycodingproblem-go/issues/674)
 * [Day 338](https://github.com/vaskoz/dailycodingproblem-go/issues/675)
 * [Day 339](https://github.com/vaskoz/dailycodingproblem-go/issues/676)
diff --git a/day334/problem.go b/day334/problem.go
new file mode 100644
index 0000000..ac6d0b8
--- /dev/null
+++ b/day334/problem.go
@@ -0,0 +1,42 @@
+package day334
+
+const goal = 24
+
+// TwentyFourGame returns true if the game of 24
+// is possible given this input.
+// Operators used are: +, -, *, and /
+func TwentyFourGame(digits []int) bool {
+	if len(digits) == 1 {
+		return digits[0] == goal
+	}
+
+	for i := 0; i < len(digits)-1; i++ {
+		for _, op := range []rune{'+', '-', '*', '/'} {
+			var next []int
+			next = append(next, digits[:i]...)
+
+			switch op {
+			case '+':
+				next = append(next, digits[i]+digits[i+1])
+			case '-':
+				next = append(next, digits[i]-digits[i+1])
+			case '*':
+				next = append(next, digits[i]*digits[i+1])
+			case '/':
+				if digits[i+1] == 0 {
+					continue
+				}
+
+				next = append(next, digits[i]/digits[i+1])
+			}
+
+			next = append(next, digits[i+2:]...)
+
+			if TwentyFourGame(next) {
+				return true
+			}
+		}
+	}
+
+	return false
+}


Test patch:
diff --git a/day334/problem_test.go b/day334/problem_test.go
new file mode 100644
index 0000000..0d7d8ee
--- /dev/null
+++ b/day334/problem_test.go
@@ -0,0 +1,32 @@
+package day334
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	digits   []int
+	expected bool
+}{
+	{[]int{5, 2, 7, 8}, true},
+	{[]int{100, 100, 100, 100, 100}, false},
+	{[]int{2, 2, 2, 2, 2, 2, 2, 2, 2}, true},
+	{[]int{5760000, 10, 30, 40, 20}, true},
+}
+
+func TestTwentyFourGame(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if result := TwentyFourGame(tc.digits); result != tc.expected {
+			t.Errorf("Given %v, expected %v, got %v", tc.digits, tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkTwentyFourGame(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			TwentyFourGame(tc.digits)
+		}
+	}
+}