Day 10. part 2.

This commit is contained in:
Borna Rajković committed 2024-12-10 10:22:05 +01:00
1 parent 778ed08680
commit 2fdf520e2b
4 files changed
+71 -11

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
012345
123456
234567
345678
4.6789
56789.
+38 -1
View File
@@ -41,7 +41,17 @@ func SolveBasic(input string) int {
// task:https://adventofcode.com/2024/day/10#part2
func SolveComplex(input string) int {
return 0
topographicMap := loadMap(input)
solutions := 0
for y, line := range topographicMap {
for x, pos := range line {
if pos == '0' {
trails := rating(topographicMap, x, y)
solutions += trails
}
}
}
return solutions
}
func score(tMap TopographicMap, x int, y int) int {
@@ -71,6 +81,33 @@ func score(tMap TopographicMap, x int, y int) int {
return len(trails)
}
func rating(tMap TopographicMap, x int, y int) int {
queue := []Point{{x, y, '0'}}
trails := 0
for len(queue) > 0 {
p := queue[0]
queue = queue[1:]
if p.height == '9' {
trails++
} else {
if tMap.At(p.x-1, p.y) == p.height+1 {
queue = append(queue, Point{p.x - 1, p.y, p.height + 1})
}
if tMap.At(p.x, p.y-1) == p.height+1 {
queue = append(queue, Point{p.x, p.y - 1, p.height + 1})
}
if tMap.At(p.x+1, p.y) == p.height+1 {
queue = append(queue, Point{p.x + 1, p.y, p.height + 1})
}
if tMap.At(p.x, p.y+1) == p.height+1 {
queue = append(queue, Point{p.x, p.y + 1, p.height + 1})
}
}
}
return trails
}
func loadMap(input string) TopographicMap {
input = strings.ReplaceAll(input, "\r", "")
topographicMap := TopographicMap{}
+19 -2
View File
@@ -18,6 +18,9 @@ var ExampleData02 string
//go:embed example_data_03.txt
var ExampleData03 string
//go:embed example_data_04.txt
var ExampleData04 string
func TestBasicSolutionExample(t *testing.T) {
result := day_10.SolveBasic(ExampleData)
if result != 36 {
@@ -48,7 +51,21 @@ func TestBasicSolutionExample03(t *testing.T) {
func TestComplexSolutionExample(t *testing.T) {
result := day_10.SolveComplex(ExampleData)
if result != 2858 {
t.Fatalf("Expected 2858 received %d", result)
if result != 81 {
t.Fatalf("Expected 81 received %d", result)
}
}
func TestComplexSolutionExample03(t *testing.T) {
result := day_10.SolveComplex(ExampleData03)
if result != 13 {
t.Fatalf("Expected 13 received %d", result)
}
}
func TestComplexSolutionExample04(t *testing.T) {
result := day_10.SolveComplex(ExampleData04)
if result != 227 {
t.Fatalf("Expected 227 received %d", result)
}
}