Skip to content

Commit

Permalink
[2024] Solution for Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 10, 2024
1 parent cbf617d commit 07cb967
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
97 changes: 97 additions & 0 deletions 2024/day10/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"fmt"
"log"
"os"

"github.com/kfarnung/advent-of-code/2024/lib"
)

func part1(input string) int64 {
topoMap, trailHeads := parseMap(input)
sum := int64(0)
for _, trailHead := range trailHeads {
terminalPositions := make(map[lib.Point2D]bool)
findTerminalPositions(topoMap, trailHead, terminalPositions)

sum += int64(len(terminalPositions))
}

return sum
}

func part2(input string) int64 {
topoMap, trailHeads := parseMap(input)
sum := int64(0)
for _, trailHead := range trailHeads {
terminalPositions := make(map[lib.Point2D]bool)
sum += findTerminalPositions(topoMap, trailHead, terminalPositions)
}

return sum
}

func findTerminalPositions(topoMap [][]rune, position lib.Point2D, terminalPositions map[lib.Point2D]bool) int64 {
current := topoMap[position.X][position.Y]
fmt.Printf("current: %v %v\n", current, position)
if current == '9' {
terminalPositions[position] = true
return 1
}

count := int64(0)
target := current + 1

if position.X > 0 && topoMap[position.X-1][position.Y] == target {
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X - 1, Y: position.Y}, terminalPositions)
}

if position.X < int64(len(topoMap)-1) && topoMap[position.X+1][position.Y] == target {
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X + 1, Y: position.Y}, terminalPositions)
}

if position.Y > 0 && topoMap[position.X][position.Y-1] == target {
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y - 1}, terminalPositions)
}

if position.Y < int64(len(topoMap[0])-1) && topoMap[position.X][position.Y+1] == target {
count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y + 1}, terminalPositions)
}

return count
}

func parseMap(input string) ([][]rune, []lib.Point2D) {
var topoMap [][]rune
var trailHeads []lib.Point2D
for i, line := range lib.SplitLines(input) {
if len(line) == 0 {
continue
}

var row []rune
for j, char := range line {
row = append(row, char)

if char == '0' {
trailHeads = append(trailHeads, lib.Point2D{X: int64(i), Y: int64(j)})
}
}

topoMap = append(topoMap, row)
}

return topoMap, trailHeads
}

func main() {
name := os.Args[1]
content, err := lib.LoadFileContent(name)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Part 1: %d\n", part1(content))
fmt.Printf("Part 2: %d\n", part2(content))
}
43 changes: 43 additions & 0 deletions 2024/day10/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"strings"
"testing"

"github.com/kfarnung/advent-of-code/2024/lib"
"github.com/stretchr/testify/assert"
)

var input = strings.Join([]string{
"89010123",
"78121874",
"87430965",
"96549874",
"45678903",
"32019012",
"01329801",
"10456732",
"",
}, "\n")

func TestPart1(t *testing.T) {
assert.Equal(t, int64(36), part1(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(593), part1(inputContent))
}

func TestPart2(t *testing.T) {
assert.Equal(t, int64(81), part2(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(1192), part2(inputContent))
}
2 changes: 1 addition & 1 deletion private

0 comments on commit 07cb967

Please sign in to comment.