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 { trails := findTrails(inp, h, h, false) 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 { trails := findTrails(inp, h, h, true) total = total + len(trails) } fmt.Println("# Part 1") fmt.Println("Scores:", total) } 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} } 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, unique) if unique { trails = append(trails, res...) } else { trails = appendUnique(trails, res...) } } } 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 }