This commit is contained in:
2025-01-14 15:33:54 +00:00
parent 225d74acf9
commit 6504b8d78c
2 changed files with 41 additions and 10 deletions

View File

@@ -4,7 +4,6 @@ import (
"adventofcode2024/utils/dijkstra"
"adventofcode2024/utils/grid2d"
"adventofcode2024/utils/inputs"
"fmt"
)
type (
@@ -21,6 +20,11 @@ type (
end Vec
grid *grid2d.Grid[Cell]
}
Cheat struct {
start Vec
end Vec
cost int
}
)
const (
@@ -40,24 +44,47 @@ var (
)
func Part1(input string) int {
count := 0
maze := Maze{}.Parse(input)
fmt.Println(maze)
// 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)
return dist
// 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 {
maze := Maze{}.Parse(input)
fmt.Println(maze)
data := inputGraph(*maze)
graph := dijkstra.CreateGraph(data)
_, count := dijkstra.GetShortestPath(data.From, data.To, graph)
return count
return 0
}
func inputGraph(maze Maze) dijkstra.InputGraph {

View File

@@ -26,6 +26,7 @@ import (
"adventofcode2024/day17"
"adventofcode2024/day18"
"adventofcode2024/day19"
"adventofcode2024/day20"
"adventofcode2024/utils"
)
@@ -90,6 +91,9 @@ func main() {
case 19:
fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
fmt.Printf("part 2: %d\n", day19.Part2(utils.Readfile(d)))
case 20:
fmt.Printf("part 1: %d\n", day20.Part1(utils.Readfile(d)))
fmt.Printf("part 2: %d\n", day20.Part2(utils.Readfile(d)))
default:
panic(fmt.Errorf("no such day: %d", d))
}