Skip to content

Commit 678b4c9

Browse files
committed
Solution for 2024, day 20
1 parent ee20f79 commit 678b4c9

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

2024/20/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/20/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2024/20/common.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"slices"
8+
)
9+
10+
type P struct{ x, y int }
11+
12+
var Delta = []P{{0, 1}, {1, 0}, {-1, 0}, {0, -1}}
13+
14+
func main() {
15+
walls, start, end := parseInput()
16+
17+
path := bfs(walls, start, end)
18+
19+
sum := 0
20+
for i := 0; i < len(path)-1; i++ {
21+
for j := i + 1; j < len(path); j++ {
22+
dist := manhattan(path[i], path[j])
23+
if dist > 0 && dist <= CheatTime {
24+
saved := j - i - dist
25+
if saved >= MinSavedTime {
26+
sum++
27+
}
28+
}
29+
}
30+
}
31+
fmt.Println(sum)
32+
}
33+
34+
func parseInput() (map[P]struct{}, P, P) {
35+
walls := map[P]struct{}{}
36+
var start, end P
37+
scanner := bufio.NewScanner(os.Stdin)
38+
for i := 0; scanner.Scan(); i++ {
39+
for j, ch := range scanner.Text() {
40+
switch ch {
41+
case '#':
42+
walls[P{i, j}] = struct{}{}
43+
case 'S':
44+
start = P{i, j}
45+
case 'E':
46+
end = P{i, j}
47+
}
48+
}
49+
}
50+
return walls, start, end
51+
}
52+
53+
func bfs(walls map[P]struct{}, start, end P) []P {
54+
queue := []P{start}
55+
visited := map[P]struct{}{start: {}}
56+
parent := map[P]P{}
57+
58+
for len(queue) > 0 {
59+
var curr P
60+
curr, queue = queue[0], queue[1:]
61+
62+
if curr == end {
63+
return getPath(parent, start, end)
64+
}
65+
66+
for _, d := range Delta {
67+
next := P{curr.x + d.x, curr.y + d.y}
68+
69+
if _, ok := walls[next]; ok {
70+
continue
71+
}
72+
73+
if _, ok := visited[next]; !ok {
74+
visited[next] = struct{}{}
75+
parent[next] = curr
76+
queue = append(queue, next)
77+
}
78+
}
79+
}
80+
return nil
81+
}
82+
83+
func getPath(parent map[P]P, start, end P) []P {
84+
l := make([]P, 0)
85+
for p := end; p != start; p = parent[p] {
86+
l = append(l, p)
87+
}
88+
l = append(l, start)
89+
slices.Reverse(l)
90+
return l
91+
}
92+
93+
func manhattan(p1, p2 P) int {
94+
return abs(p2.x-p1.x) + abs(p2.y-p1.y)
95+
}
96+
97+
func abs(x int) int {
98+
if x < 0 {
99+
return -x
100+
}
101+
return x
102+
}

2024/20/main1.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
const (
4+
CheatTime = 2
5+
MinSavedTime = 100
6+
)

2024/20/main2.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
const (
4+
CheatTime = 20
5+
MinSavedTime = 100
6+
)

0 commit comments

Comments
 (0)