Files
adventofcode/2024/go/day20/day20.go
2025-01-14 15:33:58 +00:00

156 lines
3.2 KiB
Go

package day20
import (
"adventofcode2024/utils/dijkstra"
"adventofcode2024/utils/grid2d"
"adventofcode2024/utils/inputs"
)
type (
Vec struct{ x, y int }
Dir struct{ x, y int }
Cell rune
Move struct {
p Vec
d Dir
}
Maze struct {
height, width int
start Vec
end Vec
grid *grid2d.Grid[Cell]
}
Cheat struct {
start Vec
end Vec
cost int
}
)
const (
WALL Cell = '#'
CORRIDOR Cell = '.'
START Cell = 'S'
END Cell = 'E'
PATH Cell = '*'
)
var (
NORTH = Dir{0, -1}
EAST = Dir{1, 0}
SOUTH = Dir{0, 1}
WEST = Dir{-1, 0}
DIRECTIONS = []Dir{NORTH, EAST, SOUTH, WEST}
)
func Part1(input string) int {
count := 0
maze := Maze{}.Parse(input)
// fmt.Println(maze)
data := inputGraph(*maze)
graph := dijkstra.CreateGraph(data)
path, dist := dijkstra.GetShortestPath(data.From, data.To, graph)
cost := make(map[Vec]int)
cheats := []Cheat{}
for i, p := range path {
cost[Vec{p.X, p.Y}] = i
}
for _, p := range path {
maze.grid.Set(p.X, p.Y, PATH)
for _, dir := range DIRECTIONS {
nx1, ny1 := p.X+dir.x, p.Y+dir.y
if maze.grid.Get(nx1, ny1) == WALL {
nx2, ny2 := p.X+dir.x+dir.x, p.Y+dir.y+dir.y
switch maze.grid.Get(nx2, ny2) {
case CORRIDOR:
if cost[Vec{p.X, p.Y}]+2 < cost[Vec{nx2, ny2}] {
cheats = append(cheats, Cheat{start: Vec{p.X, p.Y}, end: Vec{nx2, ny2}, cost: dist - cost[Vec{nx2, ny2}] + cost[Vec{p.X, p.Y}] + 2})
}
default:
break
}
}
}
}
// fmt.Println(maze)
for _, cheat := range cheats {
save := dist - cheat.cost
// fmt.Println("[", i, "]", "save", save)
if save >= 100 {
count++
}
}
return count
}
func Part2(input string) int {
return 0
}
func inputGraph(maze Maze) dijkstra.InputGraph {
data := dijkstra.InputGraph{}
data.From = dijkstra.Point{X: maze.start.x, Y: maze.start.y}
data.To = dijkstra.Point{X: maze.end.x, Y: maze.end.y}
for y := 0; y < maze.height; y++ {
for x := 0; x < maze.width; x++ {
if maze.grid.Get(x, y) == WALL {
continue
}
for _, dir := range DIRECTIONS {
nx, ny := x+dir.x, y+dir.y
if nx < 0 || ny < 0 || nx >= maze.width || ny >= maze.height {
continue
}
if maze.grid.Get(nx, ny) == WALL {
continue
}
data.Graph = append(data.Graph, dijkstra.InputData{
Source: dijkstra.Point{X: x, Y: y},
Destination: dijkstra.Point{X: nx, Y: ny},
Weight: 1,
})
}
}
}
return data
}
func (m Maze) Parse(input string) *Maze {
m.grid = inputs.ToGrid2D(input, "\n", "", '?', func(c string) Cell { return Cell(c[0]) })
m.height, m.width = m.grid.SizeY(), m.grid.SizeX()
for y := 0; y < m.height; y++ {
for x := 0; x < m.width; x++ {
c := m.grid.Get(x, y)
switch c {
case WALL, CORRIDOR:
m.grid.Set(x, y, c)
case START:
m.grid.Set(x, y, CORRIDOR)
m.start = Vec{x, y}
case END:
m.grid.Set(x, y, CORRIDOR)
m.end = Vec{x, y}
}
}
}
return &m
}
func (m *Maze) String() string {
s := ""
for y := 0; y < m.height; y++ {
for x := 0; x < m.width; x++ {
if y == m.start.y && x == m.start.x {
s += string(START)
} else if y == m.end.y && x == m.end.x {
s += string(END)
} else {
s += string(m.grid.Get(x, y))
}
}
s += "\n"
}
return s
}