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

Commit e4c9b19

Browse files
committed
Add solution day 10 part 1
1 parent 8de2bb6 commit e4c9b19

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ automatically rebuilt and redeployed ever time the `common` module or their own
5454
| 07 | ⭐ ⭐ | 20 | |
5555
| 08 | ⭐ ⭐ | 21 | |
5656
| 09 | | 22 | |
57-
| 10 | | 23 | |
57+
| 10 | | 23 | |
5858
| 11 | | 24 | |
5959
| 12 | | 25 | |
6060
| 13 | | | |

solutions/day10/main.go

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,82 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"github.com/terminalnode/adventofcode2024/common/util"
7+
"strings"
58
)
69

710
func main() {
8-
common.Setup(10, nil, nil)
11+
common.Setup(10, part1, nil)
12+
}
13+
14+
func part1(
15+
input string,
16+
) string {
17+
topo := parseTopographicMap(input)
18+
19+
out := 0
20+
for y := 0; y < len(topo); y++ {
21+
row := topo[y]
22+
for x := 0; x < len(row); x++ {
23+
value := topo[y][x]
24+
if value != 0 {
25+
continue
26+
}
27+
28+
nineMap := make(map[string]bool)
29+
buildMapForPart1(topo, nineMap, util.Coordinate{X: x, Y: y}, -1)
30+
out += len(nineMap)
31+
}
32+
}
33+
34+
return fmt.Sprintf("Number of trails: %d", out)
35+
}
36+
37+
func parseTopographicMap(
38+
input string,
39+
) [][]int {
40+
lines := strings.Split(input, "\n")
41+
out := make([][]int, len(lines))
42+
for y, row := range lines {
43+
out[y] = make([]int, len(row))
44+
for x, char := range row {
45+
digit := int(char) - '0'
46+
out[y][x] = digit
47+
}
48+
}
49+
50+
return out
51+
}
52+
53+
func buildMapForPart1(
54+
topographicMap [][]int,
55+
nineMap map[string]bool,
56+
position util.Coordinate,
57+
prevValue int,
58+
) {
59+
if !util.In2DArray(position, topographicMap) {
60+
return
61+
}
62+
63+
value := topographicMap[position.Y][position.X]
64+
if value != prevValue+1 {
65+
return
66+
}
67+
68+
if value == 9 {
69+
k := fmt.Sprintf("%v", position)
70+
nineMap[k] = true
71+
} else {
72+
cs := []util.Coordinate{
73+
position.North(),
74+
position.East(),
75+
position.South(),
76+
position.West(),
77+
}
78+
for _, newPos := range cs {
79+
buildMapForPart1(topographicMap, nineMap, newPos, value)
80+
}
81+
}
982
}

0 commit comments

Comments
 (0)