2024-12-10 13:41:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
inp := h.StdinToCoordMap()
|
|
|
|
part1(inp)
|
|
|
|
fmt.Println()
|
|
|
|
part2(inp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func part1(inp h.CoordByteMap) {
|
|
|
|
var total int
|
|
|
|
heads := inp.FindAll('0')
|
|
|
|
for _, h := range heads {
|
2024-12-10 13:43:58 +00:00
|
|
|
trails := findTrails(inp, h, h, false)
|
2024-12-10 13:41:44 +00:00
|
|
|
total = total + len(trails)
|
|
|
|
}
|
|
|
|
fmt.Println("# Part 1")
|
|
|
|
fmt.Println("Scores:", total)
|
|
|
|
}
|
|
|
|
|
|
|
|
func part2(inp h.CoordByteMap) {
|
|
|
|
var total int
|
|
|
|
heads := inp.FindAll('0')
|
|
|
|
for _, h := range heads {
|
2024-12-10 13:43:58 +00:00
|
|
|
trails := findTrails(inp, h, h, true)
|
2024-12-10 13:41:44 +00:00
|
|
|
total = total + len(trails)
|
|
|
|
}
|
|
|
|
fmt.Println("# Part 1")
|
|
|
|
fmt.Println("Scores:", total)
|
|
|
|
}
|
|
|
|
|
2024-12-10 13:43:58 +00:00
|
|
|
func findTrails(m h.CoordByteMap, st h.Coordinate, curr h.Coordinate, unique bool) []h.Coordinate {
|
2024-12-10 13:41:44 +00:00
|
|
|
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 {
|
2024-12-10 13:43:58 +00:00
|
|
|
res := findTrails(m, st, nx, unique)
|
|
|
|
if unique {
|
|
|
|
trails = append(trails, res...)
|
|
|
|
} else {
|
|
|
|
trails = appendUnique(trails, res...)
|
|
|
|
}
|
2024-12-10 13:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return trails
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendUnique(sl []h.Coordinate, n ...h.Coordinate) []h.Coordinate {
|
|
|
|
if len(n) == 0 {
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
if len(n) > 1 {
|
|
|
|
for j := range n {
|
|
|
|
sl = appendUnique(sl, n[j])
|
|
|
|
}
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range sl {
|
|
|
|
if sl[i].Equals(n[0]) {
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sl = append(sl, n[0])
|
|
|
|
return sl
|
|
|
|
}
|