Skip to content

Commit 07cb967

Browse files
committed
[2024] Solution for Day 10
1 parent cbf617d commit 07cb967

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

2024/day10/main.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/kfarnung/advent-of-code/2024/lib"
9+
)
10+
11+
func part1(input string) int64 {
12+
topoMap, trailHeads := parseMap(input)
13+
sum := int64(0)
14+
for _, trailHead := range trailHeads {
15+
terminalPositions := make(map[lib.Point2D]bool)
16+
findTerminalPositions(topoMap, trailHead, terminalPositions)
17+
18+
sum += int64(len(terminalPositions))
19+
}
20+
21+
return sum
22+
}
23+
24+
func part2(input string) int64 {
25+
topoMap, trailHeads := parseMap(input)
26+
sum := int64(0)
27+
for _, trailHead := range trailHeads {
28+
terminalPositions := make(map[lib.Point2D]bool)
29+
sum += findTerminalPositions(topoMap, trailHead, terminalPositions)
30+
}
31+
32+
return sum
33+
}
34+
35+
func findTerminalPositions(topoMap [][]rune, position lib.Point2D, terminalPositions map[lib.Point2D]bool) int64 {
36+
current := topoMap[position.X][position.Y]
37+
fmt.Printf("current: %v %v\n", current, position)
38+
if current == '9' {
39+
terminalPositions[position] = true
40+
return 1
41+
}
42+
43+
count := int64(0)
44+
target := current + 1
45+
46+
if position.X > 0 && topoMap[position.X-1][position.Y] == target {
47+
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X - 1, Y: position.Y}, terminalPositions)
48+
}
49+
50+
if position.X < int64(len(topoMap)-1) && topoMap[position.X+1][position.Y] == target {
51+
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X + 1, Y: position.Y}, terminalPositions)
52+
}
53+
54+
if position.Y > 0 && topoMap[position.X][position.Y-1] == target {
55+
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y - 1}, terminalPositions)
56+
}
57+
58+
if position.Y < int64(len(topoMap[0])-1) && topoMap[position.X][position.Y+1] == target {
59+
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y + 1}, terminalPositions)
60+
}
61+
62+
return count
63+
}
64+
65+
func parseMap(input string) ([][]rune, []lib.Point2D) {
66+
var topoMap [][]rune
67+
var trailHeads []lib.Point2D
68+
for i, line := range lib.SplitLines(input) {
69+
if len(line) == 0 {
70+
continue
71+
}
72+
73+
var row []rune
74+
for j, char := range line {
75+
row = append(row, char)
76+
77+
if char == '0' {
78+
trailHeads = append(trailHeads, lib.Point2D{X: int64(i), Y: int64(j)})
79+
}
80+
}
81+
82+
topoMap = append(topoMap, row)
83+
}
84+
85+
return topoMap, trailHeads
86+
}
87+
88+
func main() {
89+
name := os.Args[1]
90+
content, err := lib.LoadFileContent(name)
91+
if err != nil {
92+
log.Fatal(err)
93+
}
94+
95+
fmt.Printf("Part 1: %d\n", part1(content))
96+
fmt.Printf("Part 2: %d\n", part2(content))
97+
}

2024/day10/main_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/kfarnung/advent-of-code/2024/lib"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var input = strings.Join([]string{
12+
"89010123",
13+
"78121874",
14+
"87430965",
15+
"96549874",
16+
"45678903",
17+
"32019012",
18+
"01329801",
19+
"10456732",
20+
"",
21+
}, "\n")
22+
23+
func TestPart1(t *testing.T) {
24+
assert.Equal(t, int64(36), part1(input))
25+
26+
inputContent, err := lib.GetInputContent()
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
assert.Equal(t, int64(593), part1(inputContent))
32+
}
33+
34+
func TestPart2(t *testing.T) {
35+
assert.Equal(t, int64(81), part2(input))
36+
37+
inputContent, err := lib.GetInputContent()
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
assert.Equal(t, int64(1192), part2(inputContent))
43+
}

private

0 commit comments

Comments
 (0)