refactor d9
This commit is contained in:
parent
b5444e5aa5
commit
0338b14930
2 changed files with 16 additions and 28 deletions
|
@ -5,11 +5,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const wall = '9'
|
||||||
|
|
||||||
func addWalls(lines []string) []string {
|
func addWalls(lines []string) []string {
|
||||||
for i, line := range lines {
|
for i, line := range lines {
|
||||||
lines[i] = fmt.Sprintf("9%v9", line)
|
lines[i] = fmt.Sprintf("%v%v%v", string(wall), line, string(wall))
|
||||||
}
|
}
|
||||||
|
nines := strings.Repeat(string(wall), len(lines[0]))
|
||||||
nines := strings.Repeat("9", len(lines[0]))
|
|
||||||
return append([]string{nines}, append(lines, nines)...)
|
return append([]string{nines}, append(lines, nines)...)
|
||||||
}
|
}
|
||||||
|
|
37
d9/p2.go
37
d9/p2.go
|
@ -5,54 +5,41 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type exists struct{}
|
|
||||||
|
|
||||||
type pos struct {
|
type pos struct {
|
||||||
x int
|
x int
|
||||||
y int
|
y int
|
||||||
}
|
}
|
||||||
|
|
||||||
type basin map[pos]exists
|
type basin map[pos]bool
|
||||||
|
|
||||||
const wall = '9'
|
func flood(x, y int, heights []string, basin basin, flooded basin) basin {
|
||||||
|
|
||||||
func flooded(x, y int, basins []basin) bool {
|
|
||||||
for _, basin := range basins {
|
|
||||||
if _, ok := basin[pos{x, y}]; ok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func flood(x, y int, heights []string, basin basin) basin {
|
|
||||||
if heights[y][x] == wall {
|
if heights[y][x] == wall {
|
||||||
return basin
|
return basin
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := basin[pos{x, y}]; ok {
|
if flooded[pos{x, y}] {
|
||||||
return basin
|
return basin
|
||||||
}
|
}
|
||||||
|
|
||||||
basin[pos{x, y}] = exists{}
|
basin[pos{x, y}] = true
|
||||||
|
flooded[pos{x, y}] = true
|
||||||
|
|
||||||
|
flood(x, y-1, heights, basin, flooded)
|
||||||
|
flood(x+1, y, heights, basin, flooded)
|
||||||
|
flood(x, y+1, heights, basin, flooded)
|
||||||
|
flood(x-1, y, heights, basin, flooded)
|
||||||
|
|
||||||
flood(x, y-1, heights, basin)
|
|
||||||
flood(x+1, y, heights, basin)
|
|
||||||
flood(x, y+1, heights, basin)
|
|
||||||
flood(x-1, y, heights, basin)
|
|
||||||
return basin
|
return basin
|
||||||
}
|
}
|
||||||
|
|
||||||
func P2(in string) int {
|
func P2(in string) int {
|
||||||
heights := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
heights := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
||||||
basins := []basin{}
|
basins := []basin{}
|
||||||
|
flooded := basin{}
|
||||||
|
|
||||||
for y := 1; y < len(heights)-1; y++ {
|
for y := 1; y < len(heights)-1; y++ {
|
||||||
for x := 1; x < len(heights[0])-1; x++ {
|
for x := 1; x < len(heights[0])-1; x++ {
|
||||||
if !flooded(x, y, basins) && heights[y][x] != wall {
|
basins = append(basins, flood(x, y, heights, basin{}, flooded))
|
||||||
basins = append(basins, flood(x, y, heights, basin{}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue