-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from vaskoz/day151
Day151
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |