day20
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"adventofcode2024/utils/dijkstra"
|
"adventofcode2024/utils/dijkstra"
|
||||||
"adventofcode2024/utils/grid2d"
|
"adventofcode2024/utils/grid2d"
|
||||||
"adventofcode2024/utils/inputs"
|
"adventofcode2024/utils/inputs"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -21,6 +20,11 @@ type (
|
|||||||
end Vec
|
end Vec
|
||||||
grid *grid2d.Grid[Cell]
|
grid *grid2d.Grid[Cell]
|
||||||
}
|
}
|
||||||
|
Cheat struct {
|
||||||
|
start Vec
|
||||||
|
end Vec
|
||||||
|
cost int
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -40,24 +44,47 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Part1(input string) int {
|
func Part1(input string) int {
|
||||||
|
count := 0
|
||||||
maze := Maze{}.Parse(input)
|
maze := Maze{}.Parse(input)
|
||||||
fmt.Println(maze)
|
// fmt.Println(maze)
|
||||||
data := inputGraph(*maze)
|
data := inputGraph(*maze)
|
||||||
graph := dijkstra.CreateGraph(data)
|
graph := dijkstra.CreateGraph(data)
|
||||||
path, dist := dijkstra.GetShortestPath(data.From, data.To, graph)
|
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 {
|
for _, p := range path {
|
||||||
maze.grid.Set(p.X, p.Y, 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)
|
// fmt.Println(maze)
|
||||||
return dist
|
for _, cheat := range cheats {
|
||||||
|
save := dist - cheat.cost
|
||||||
|
// fmt.Println("[", i, "]", "save", save)
|
||||||
|
if save >= 100 {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
}
|
}
|
||||||
func Part2(input string) int {
|
func Part2(input string) int {
|
||||||
maze := Maze{}.Parse(input)
|
return 0
|
||||||
fmt.Println(maze)
|
|
||||||
data := inputGraph(*maze)
|
|
||||||
graph := dijkstra.CreateGraph(data)
|
|
||||||
_, count := dijkstra.GetShortestPath(data.From, data.To, graph)
|
|
||||||
return count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func inputGraph(maze Maze) dijkstra.InputGraph {
|
func inputGraph(maze Maze) dijkstra.InputGraph {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"adventofcode2024/day17"
|
"adventofcode2024/day17"
|
||||||
"adventofcode2024/day18"
|
"adventofcode2024/day18"
|
||||||
"adventofcode2024/day19"
|
"adventofcode2024/day19"
|
||||||
|
"adventofcode2024/day20"
|
||||||
"adventofcode2024/utils"
|
"adventofcode2024/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,6 +91,9 @@ func main() {
|
|||||||
case 19:
|
case 19:
|
||||||
fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
|
fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
|
||||||
fmt.Printf("part 2: %d\n", day19.Part2(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:
|
default:
|
||||||
panic(fmt.Errorf("no such day: %d", d))
|
panic(fmt.Errorf("no such day: %d", d))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user