Skip to content

Commit

Permalink
Merge pull request #318 from vaskoz/day151
Browse files Browse the repository at this point in the history
Day151
  • Loading branch information
vaskoz committed Jan 23, 2019
2 parents 4722dbe + 49b5d16 commit 421fffa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
31 changes: 31 additions & 0 deletions day151/problem.go
Original file line number Diff line number Diff line change
@@ -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
}
}
58 changes: 58 additions & 0 deletions day151/problem_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 421fffa

Please sign in to comment.