Added 12. part 2. fast (10x) solution

This commit is contained in:
Borna Rajković committed 2024-12-12 19:49:03 +01:00
1 parent 26a7b87c31
commit 0af5c11e6d
2 files changed
+83 -4

No files matched your search

+76 -4
View File
@@ -47,16 +47,16 @@ func SolveBasic(input string) int {
// mark as visited to not visit it more than once
visited[point] = true
if _, present := field[Point{point.x - 1, point.y}]; !present {
if farmMap.At(point.x-1, point.y) != value {
parameter++
}
if _, present := field[Point{point.x + 1, point.y}]; !present {
if farmMap.At(point.x+1, point.y) != value {
parameter++
}
if _, present := field[Point{point.x, point.y - 1}]; !present {
if farmMap.At(point.x, point.y-1) != value {
parameter++
}
if _, present := field[Point{point.x, point.y + 1}]; !present {
if farmMap.At(point.x, point.y+1) != value {
parameter++
}
}
@@ -96,6 +96,35 @@ func SolveComplex(input string) int {
return sum
}
// task:https://adventofcode.com/2024/day/12#part2
// there is no need to iterate over the whole table,
// more importantly there is no need to look at the set as we at the Map
func SolveComplexFast(input string) int {
farmMap := loadMap(input)
visited := Visited{}
sum := 0
for y, row := range farmMap {
for x, value := range row {
p := Point{x, y}
field := Visited{}
if _, present := visited[p]; !present {
visit(farmMap, field, x, y, value)
area := len(field)
parameter := countEdgesFast(farmMap, field, value)
for point := range field {
// mark as visited to not visit it more than once
visited[point] = true
}
sum += area * parameter
}
}
}
return sum
}
func countEdges(farmMap Map, field Visited) int {
borders := map[BorderPoint]bool{}
edges := 0
@@ -133,6 +162,49 @@ func countEdges(farmMap Map, field Visited) int {
return edges
}
func countEdgesFast(farmMap Map, field Visited, value rune) int {
edges := 0
for p := range field {
hasN := farmMap.At(p.x, p.y-1) == value
hasW := farmMap.At(p.x-1, p.y) == value
hasS := farmMap.At(p.x, p.y+1) == value
hasE := farmMap.At(p.x+1, p.y) == value
hasNW := farmMap.At(p.x-1, p.y-1) == value
hasNE := farmMap.At(p.x+1, p.y-1) == value
hasSW := farmMap.At(p.x-1, p.y+1) == value
hasSE := farmMap.At(p.x+1, p.y+1) == value
// inner corner
if hasN && hasW && !hasNW {
edges++
}
if hasN && hasE && !hasNE {
edges++
}
if hasS && hasW && !hasSW {
edges++
}
if hasS && hasE && !hasSE {
edges++
}
// outer corner
if !hasN && !hasW {
edges++
}
if !hasN && !hasE {
edges++
}
if !hasS && !hasW {
edges++
}
if !hasS && !hasE {
edges++
}
}
return edges
}
type Direction int
const (