-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Submodule private
updated
from 75994f to d43ed9