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

Commit 6bda09c

Browse files
committed
Add solution day 10 part 2
1 parent e4c9b19 commit 6bda09c

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-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

+53-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func main() {
11-
common.Setup(10, part1, nil)
11+
common.Setup(10, part1, part2)
1212
}
1313

1414
func part1(
@@ -34,6 +34,27 @@ func part1(
3434
return fmt.Sprintf("Number of trails: %d", out)
3535
}
3636

37+
func part2(
38+
input string,
39+
) string {
40+
topo := parseTopographicMap(input)
41+
42+
out := 0
43+
for y := 0; y < len(topo); y++ {
44+
row := topo[y]
45+
for x := 0; x < len(row); x++ {
46+
value := topo[y][x]
47+
if value != 0 {
48+
continue
49+
}
50+
51+
out += countDistinctTrails(topo, util.Coordinate{X: x, Y: y}, 0)
52+
}
53+
}
54+
55+
return fmt.Sprintf("Number of distinct trails: %d", out)
56+
}
57+
3758
func parseTopographicMap(
3859
input string,
3960
) [][]int {
@@ -80,3 +101,34 @@ func buildMapForPart1(
80101
}
81102
}
82103
}
104+
105+
func countDistinctTrails(
106+
topographicMap [][]int,
107+
position util.Coordinate,
108+
value int,
109+
) int {
110+
if value == 9 {
111+
return 1
112+
}
113+
114+
cs := []util.Coordinate{
115+
position.North(),
116+
position.East(),
117+
position.South(),
118+
position.West(),
119+
}
120+
121+
out := 0
122+
for _, c := range cs {
123+
if !util.In2DArray(c, topographicMap) {
124+
continue
125+
}
126+
127+
cValue := topographicMap[c.Y][c.X]
128+
if cValue == value+1 {
129+
out += countDistinctTrails(topographicMap, c, cValue)
130+
}
131+
}
132+
133+
return out
134+
}

0 commit comments

Comments
 (0)