Merge remote-tracking branch 'origin/master' into master

This commit is contained in:
Stefan Harmuth 2020-12-30 17:23:28 +01:00
commit 6aa66b4f6f

View File

@ -4,20 +4,15 @@ import (
"tools" "tools"
) )
type coord struct { func play(grid tools.GridToggle, stuckCorners bool) {
x int neighbours := tools.NewGridInt()
y int for _, thisCoord := range grid.GetCoordinates() {
}
func play(grid tools.ToggleGrid, stuckCorners bool) {
neighbours := make(map[coord]int)
for thisCoord := range grid.GetOnCoordinates() {
for cx := thisCoord.X - 1; cx <= thisCoord.X+1; cx++ { for cx := thisCoord.X - 1; cx <= thisCoord.X+1; cx++ {
for cy := thisCoord.Y - 1; cy <= thisCoord.Y+1; cy++ { for cy := thisCoord.Y - 1; cy <= thisCoord.Y+1; cy++ {
if cy == thisCoord.Y && cx == thisCoord.X { if cy == thisCoord.Y && cx == thisCoord.X {
continue continue
} }
neighbours[coord{cx, cy}]++ neighbours.Add(cx, cy, 1)
} }
} }
} }
@ -25,20 +20,20 @@ func play(grid tools.ToggleGrid, stuckCorners bool) {
for x := 0; x <= grid.MaxX; x++ { for x := 0; x <= grid.MaxX; x++ {
for y := 0; y <= grid.MaxY; y++ { for y := 0; y <= grid.MaxY; y++ {
if grid.State(x, y) { if grid.State(x, y) {
if neighbours[coord{x, y}] != 2 && neighbours[coord{x, y}] != 3 { if neighbours.Value(x, y) != 2 && neighbours.Value(x, y) != 3 {
if !stuckCorners || !grid.IsCorner(x, y) { if !stuckCorners || !grid.IsCorner(x, y) {
grid.Set(x, y, tools.Off) grid.Set(x, y, tools.Off)
} }
} }
} else if neighbours[coord{x, y}] == 3 { } else if neighbours.Value(x, y) == 3 {
grid.Set(x, y, tools.On) grid.Set(x, y, tools.On)
} }
} }
} }
} }
func makeGridFromInput(input []string) tools.ToggleGrid { func makeGridFromInput(input []string) tools.GridToggle {
grid := tools.NewToogleGrid() grid := tools.NewGridToggle()
for y, line := range input { for y, line := range input {
for x, c := range line { for x, c := range line {
if c == '#' { if c == '#' {