Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 151
Good morning! Here's your coding interview problem for today.

Given a 2-D matrix representing an image, a location of a pixel in the screen and a color C, replace the color of the given pixel and all adjacent same colored pixels with C.

For example, given the following matrix, and location pixel of (2, 2), and 'G' for green:
```
B B W
W W W
W W W
B B B
```
Becomes
```
B B G
G G G
G G G
B B B
```

Pull request description:
Day151
Resolves #313.

Patch:
diff --git a/README.md b/README.md
index c3080e7..be9bf1c 100644
--- a/README.md
+++ b/README.md
@@ -161,3 +161,4 @@ problems from
 * [Day 148](https://github.com/vaskoz/dailycodingproblem-go/issues/309)
 * [Day 149](https://github.com/vaskoz/dailycodingproblem-go/issues/311)
 * [Day 150](https://github.com/vaskoz/dailycodingproblem-go/issues/312)
+* [Day 151](https://github.com/vaskoz/dailycodingproblem-go/issues/313)
diff --git a/day151/problem.go b/day151/problem.go
new file mode 100644
index 0000000..a2930ec
--- /dev/null
+++ b/day151/problem.go
@@ -0,0 +1,31 @@
+package day151
+
+// PixelGrid is a 2-D grid of pixel colors.
+type PixelGrid [][]Color
+
+// Coordinate represents a pixel location in the grid.
+type Coordinate struct {
+	Row, Col int
+}
+
+// Color represents a unique color that a pixel can have.
+type Color rune
+
+// ReplaceAdjacentColorPixel changes all the pixels adjacent to the given coordinate
+// of the same color to the new color.
+func ReplaceAdjacentColorPixel(grid PixelGrid, c Coordinate, color Color) {
+	start := grid[c.Row][c.Col]
+	replaceAdjacentColorPixel(grid, c, start, color)
+}
+
+func replaceAdjacentColorPixel(grid PixelGrid, c Coordinate, start, target Color) {
+	if c.Row < 0 || c.Col < 0 || c.Row >= len(grid) || c.Col >= len(grid[0]) {
+		return
+	} else if grid[c.Row][c.Col] == start {
+		grid[c.Row][c.Col] = target
+		replaceAdjacentColorPixel(grid, Coordinate{c.Row - 1, c.Col}, start, target) // up
+		replaceAdjacentColorPixel(grid, Coordinate{c.Row + 1, c.Col}, start, target) // down
+		replaceAdjacentColorPixel(grid, Coordinate{c.Row, c.Col - 1}, start, target) // left
+		replaceAdjacentColorPixel(grid, Coordinate{c.Row, c.Col + 1}, start, target) // right
+	}
+}


Test patch:
diff --git a/day151/problem_test.go b/day151/problem_test.go
new file mode 100644
index 0000000..42e3f35
--- /dev/null
+++ b/day151/problem_test.go
@@ -0,0 +1,58 @@
+package day151
+
+import (
+	"reflect"
+	"testing"
+)
+
+var testcases = []struct {
+	grid     PixelGrid
+	coord    Coordinate
+	color    Color
+	expected PixelGrid
+}{
+	{
+		PixelGrid{
+			[]Color("BBW"),
+			[]Color("WWW"),
+			[]Color("WWW"),
+			[]Color("BBB"),
+		},
+		Coordinate{2, 2},
+		Color('G'),
+		PixelGrid{
+			[]Color("BBG"),
+			[]Color("GGG"),
+			[]Color("GGG"),
+			[]Color("BBB"),
+		},
+	},
+}
+
+func TestReplaceAdjacentColorPixel(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		testGrid := copyGrid(tc.grid)
+		ReplaceAdjacentColorPixel(testGrid, tc.coord, tc.color)
+		if !reflect.DeepEqual(tc.expected, testGrid) {
+			t.Errorf("Expected %v got %v", tc.expected, testGrid)
+		}
+	}
+}
+
+func BenchmarkReplaceAdjacentColorPixel(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			testGrid := copyGrid(tc.grid)
+			ReplaceAdjacentColorPixel(testGrid, tc.coord, tc.color)
+		}
+	}
+}
+
+func copyGrid(grid PixelGrid) PixelGrid {
+	var newGrid PixelGrid
+	for i := range grid {
+		newGrid = append(newGrid, append([]Color{}, grid[i]...))
+	}
+	return newGrid
+}