adventofcode/2024/day20/main.go

54 lines
1.0 KiB
Go

package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
solve(inp)
}
var (
N = h.Coordinate{X: 0, Y: -1}
E = h.Coordinate{X: 1, Y: 0}
S = h.Coordinate{X: 0, Y: 1}
W = h.Coordinate{X: -1, Y: 0}
Dirs = []h.Coordinate{N, E, S, W}
)
func solve(inp []string) {
m := h.StringSliceToCoordByteMap(inp)
start, _ := m.FindFirst('S')
// Find the path through the map
queue, dist := []h.Coordinate{start}, map[h.Coordinate]int{start: 0}
for len(queue) > 0 {
p := queue[0]
queue = queue[1:]
for _, d := range Dirs {
n := p.Add(d)
if _, ok := dist[n]; !ok && m.Get(n) != '#' {
queue, dist[n] = append(queue, n), dist[p]+1
}
}
}
part1, part2 := 0, 0
for p1 := range dist {
for p2 := range dist {
d := p1.Distance(p2)
if d <= 20 && dist[p2] >= dist[p1]+d+100 {
if d <= 2 {
part1++
}
part2++
}
}
}
fmt.Println("# Part 1")
fmt.Println(part1)
fmt.Println("\n# Part 2")
fmt.Println(part2)
}