Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 312
Good morning! Here's your coding interview problem for today.

This problem was asked by Wayfair.

You are given a `2 x N` board, and instructed to completely cover the board with the following shapes:

Dominoes, or `2 x 1` rectangles.
Trominoes, or `L`-shapes.
For example, if `N = 4`, here is one possible configuration, where A is a domino, and B and C are trominoes.
```
A B B C
A B C C
```
Given an integer `N`, determine in how many ways this task is possible.

Pull request description:
Day312
Resolves #637.

Patch:
diff --git a/README.md b/README.md
index a160d15..45a3dc7 100644
--- a/README.md
+++ b/README.md
@@ -319,3 +319,4 @@ problems from
 * [Day 309](https://github.com/vaskoz/dailycodingproblem-go/issues/631)
 * [Day 310](https://github.com/vaskoz/dailycodingproblem-go/issues/633)
 * [Day 311](https://github.com/vaskoz/dailycodingproblem-go/issues/635)
+* [Day 312](https://github.com/vaskoz/dailycodingproblem-go/issues/637)
diff --git a/day312/problem.go b/day312/problem.go
new file mode 100644
index 0000000..39d7697
--- /dev/null
+++ b/day312/problem.go
@@ -0,0 +1,33 @@
+package day312
+
+const panicMessage = "n must be greater than 0"
+
+// TilingBoardWays returns the number of ways a board of size
+// 2 x N can be tiled using dominoes and trominoes.
+func TilingBoardWays(n int) int {
+	switch {
+	case n < 1:
+		panic(panicMessage)
+	case n == 1:
+		return 1
+	case n == 2:
+		return 2
+	default:
+		return TilingBoardWays(n-1) + TilingBoardWays(n-2)
+	}
+}
+
+// TilingBoardWaysFaster returns the number of ways a board of size
+// 2 x N can be tiled using dominoes and trominoes.
+func TilingBoardWaysFaster(n int) int {
+	if n < 1 {
+		panic("n must be greater than 0")
+	}
+	n1, n2 := 0, 1
+	for i := 1; i <= n; i++ {
+		tmp := n2
+		n2 = n1 + n2
+		n1 = tmp
+	}
+	return n2
+}


Test patch:
diff --git a/day312/problem_test.go b/day312/problem_test.go
new file mode 100644
index 0000000..22cc655
--- /dev/null
+++ b/day312/problem_test.go
@@ -0,0 +1,69 @@
+package day312
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	n          int
+	tilingWays int
+}{
+	{1, 1},
+	{2, 2},
+	{3, 3},
+	{4, 5},
+	{5, 8},
+}
+
+func TestTilingBoardWaysFaster(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if ways := TilingBoardWaysFaster(tc.n); ways != tc.tilingWays {
+			t.Errorf("Expected %v, got %v", tc.tilingWays, ways)
+		}
+	}
+}
+
+func TestTilingBoardWaysFasterBadInput(t *testing.T) {
+	t.Parallel()
+	defer func() {
+		if err := recover(); err == nil {
+			t.Errorf("Expected a panic for a value less than 1")
+		}
+	}()
+	TilingBoardWaysFaster(0)
+}
+
+func BenchmarkTilingBoardWaysFaster(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			TilingBoardWaysFaster(tc.n)
+		}
+	}
+}
+
+func TestTilingBoardWays(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if ways := TilingBoardWays(tc.n); ways != tc.tilingWays {
+			t.Errorf("Expected %v, got %v", tc.tilingWays, ways)
+		}
+	}
+}
+
+func TestTilingBoardWaysBadInput(t *testing.T) {
+	t.Parallel()
+	defer func() {
+		if err := recover(); err == nil {
+			t.Errorf("Expected a panic for a value less than 1")
+		}
+	}()
+	TilingBoardWays(0)
+}
+
+func BenchmarkTilingBoardWays(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			TilingBoardWays(tc.n)
+		}
+	}
+}