This commit is contained in:
Brian Buller 2024-12-10 07:43:58 -06:00
parent 00e23e364a
commit 8e7262ad45

View File

@ -17,7 +17,7 @@ func part1(inp h.CoordByteMap) {
var total int
heads := inp.FindAll('0')
for _, h := range heads {
trails := findTrails(inp, h, h)
trails := findTrails(inp, h, h, false)
total = total + len(trails)
}
fmt.Println("# Part 1")
@ -28,14 +28,14 @@ func part2(inp h.CoordByteMap) {
var total int
heads := inp.FindAll('0')
for _, h := range heads {
trails := findUniqueTrails(inp, h, h)
trails := findTrails(inp, h, h, true)
total = total + len(trails)
}
fmt.Println("# Part 1")
fmt.Println("Scores:", total)
}
func findUniqueTrails(m h.CoordByteMap, st h.Coordinate, curr h.Coordinate) []h.Coordinate {
func findTrails(m h.CoordByteMap, st h.Coordinate, curr h.Coordinate, unique bool) []h.Coordinate {
currBt := m.Get(curr)
if currBt == '9' {
return []h.Coordinate{curr}
@ -44,24 +44,12 @@ func findUniqueTrails(m h.CoordByteMap, st h.Coordinate, curr h.Coordinate) []h.
var trails []h.Coordinate
for _, nx := range []h.Coordinate{curr.North(), curr.East(), curr.South(), curr.West()} {
if m.ContainsCoord(nx) && m.Get(nx) == currBt+1 {
res := findUniqueTrails(m, st, nx)
trails = append(trails, res...)
}
}
return trails
}
func findTrails(m h.CoordByteMap, st h.Coordinate, curr h.Coordinate) []h.Coordinate {
currBt := m.Get(curr)
if currBt == '9' {
return []h.Coordinate{curr}
}
var trails []h.Coordinate
for _, nx := range []h.Coordinate{curr.North(), curr.East(), curr.South(), curr.West()} {
if m.ContainsCoord(nx) && m.Get(nx) == currBt+1 {
res := findTrails(m, st, nx)
trails = appendUnique(trails, res...)
res := findTrails(m, st, nx, unique)
if unique {
trails = append(trails, res...)
} else {
trails = appendUnique(trails, res...)
}
}
}
return trails