Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit ca5d2a8

Browse files
committed
Add solution day 18 part 1
1 parent 75eabce commit ca5d2a8

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ automatically rebuilt and redeployed every time the `common` module or their own
5050
| 02 | ⭐ ⭐ | 15 | ⭐ ⭐ |
5151
| 03 | ⭐ ⭐ | 16 | ⭐ 🥸 |
5252
| 04 | ⭐ ⭐ | 17 ||
53-
| 05 | ⭐ ⭐ | 18 | |
53+
| 05 | ⭐ ⭐ | 18 | |
5454
| 06 | ⭐ ⭐ | 19 | |
5555
| 07 | ⭐ ⭐ | 20 | |
5656
| 08 | ⭐ ⭐ | 21 | |

solutions/day18/main.go

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,108 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"github.com/terminalnode/adventofcode2024/common/util"
57
)
68

79
func main() {
8-
common.Setup(18, nil, nil)
10+
common.Setup(18, part1, nil)
11+
}
12+
13+
func part1(
14+
input string,
15+
) string {
16+
out, err := run(input, 70, 70, 1024)
17+
if err != nil {
18+
return err.Error()
19+
}
20+
return fmt.Sprintf("Shortest path is %d steps", out)
21+
}
22+
23+
func run(
24+
input string,
25+
maxX int,
26+
maxY int,
27+
fallenBytes int,
28+
) (int, error) {
29+
m := buildMatrix(maxX, maxY)
30+
cs, err := parse(input)
31+
if err != nil {
32+
return -1, err
33+
}
34+
35+
if len(cs) > fallenBytes {
36+
cs = cs[:fallenBytes]
37+
}
38+
39+
for _, c := range cs {
40+
m[c.Y][c.X] = true
41+
}
42+
43+
visitedSet := buildVisitedSet(maxY)
44+
shortestPath(
45+
m,
46+
util.Coordinate{X: 0, Y: 0},
47+
util.Coordinate{X: maxX, Y: maxY},
48+
0,
49+
visitedSet,
50+
)
51+
52+
return visitedSet[maxY][maxX], nil
53+
}
54+
55+
func buildMatrix(
56+
maxX int,
57+
maxY int,
58+
) [][]bool {
59+
out := make([][]bool, maxY+1)
60+
for y := 0; y <= maxY; y++ {
61+
out[y] = make([]bool, maxX+1)
62+
}
63+
return out
64+
}
65+
66+
func buildVisitedSet(
67+
maxY int,
68+
) map[int]map[int]int {
69+
visitedSet := make(map[int]map[int]int)
70+
for y := 0; y <= maxY; y++ {
71+
visitedSet[y] = make(map[int]int)
72+
}
73+
return visitedSet
74+
}
75+
76+
func shortestPath(
77+
m [][]bool,
78+
c util.Coordinate,
79+
goal util.Coordinate,
80+
steps int,
81+
visitedSet map[int]map[int]int,
82+
) {
83+
visit := visitedSet[c.Y][c.X]
84+
if visit > 0 && visit <= steps {
85+
// Someone beat us to this position
86+
return
87+
}
88+
89+
endVisit := visitedSet[goal.Y][goal.X]
90+
if endVisit > 0 && endVisit <= steps {
91+
// Someone beat us to the end goal
92+
return
93+
}
94+
95+
if !util.In2DArray(c, m) || m[c.Y][c.X] {
96+
// The current position is impassable or outside the matrix
97+
return
98+
}
99+
100+
// Register our place on the map
101+
visitedSet[c.Y][c.X] = steps
102+
103+
// Travel new places
104+
shortestPath(m, c.North(), goal, steps+1, visitedSet)
105+
shortestPath(m, c.East(), goal, steps+1, visitedSet)
106+
shortestPath(m, c.South(), goal, steps+1, visitedSet)
107+
shortestPath(m, c.West(), goal, steps+1, visitedSet)
9108
}

solutions/day18/parse.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/terminalnode/adventofcode2024/common/util"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func parse(
11+
input string,
12+
) ([]util.Coordinate, error) {
13+
split := strings.Split(input, "\n")
14+
out := make([]util.Coordinate, len(split))
15+
for i, s := range split {
16+
xy := strings.Split(s, ",")
17+
if len(xy) != 2 {
18+
return out, fmt.Errorf("expected '%s' split by ',' to become a size 2 list", s)
19+
}
20+
21+
x, err := strconv.ParseInt(xy[0], 10, 0)
22+
if err != nil {
23+
return out, err
24+
}
25+
26+
y, err := strconv.ParseInt(xy[1], 10, 0)
27+
if err != nil {
28+
return out, err
29+
}
30+
31+
out[i] = util.Coordinate{X: int(x), Y: int(y)}
32+
}
33+
34+
return out, nil
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy