Day 10. part 2.

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

View File

@ -0,0 +1,6 @@
012345
123456
234567
345678
4.6789
56789.

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{}

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)
}
}

16
main.go
View File

@ -45,21 +45,21 @@ func main() {
case "06-complex":
fmt.Printf("Day 06. Complex: %d\n", day_06.SolveComplex(Day06Data)) //
case "07-basic":
fmt.Printf("Day 07. Basic: %d\n", day_07.SolveBasic(Day07Data)) // 20665830408335
fmt.Printf("Day 07. Basic: %d\n", day_07.SolveBasic(Day07Data)) //
case "07-complex":
fmt.Printf("Day 07. Complex: %d\n", day_07.SolveComplex(Day07Data)) // 20665830408335
fmt.Printf("Day 07. Complex: %d\n", day_07.SolveComplex(Day07Data)) //
case "08-basic":
fmt.Printf("Day 08. Basic: %d\n", day_08.SolveBasic(Day08Data)) // 20665830408335
fmt.Printf("Day 08. Basic: %d\n", day_08.SolveBasic(Day08Data)) //
case "08-complex":
fmt.Printf("Day 08. Complex: %d\n", day_08.SolveComplex(Day08Data)) // 20665830408335
fmt.Printf("Day 08. Complex: %d\n", day_08.SolveComplex(Day08Data)) //
case "09-basic":
fmt.Printf("Day 09. Basic: %d\n", day_09.SolveBasic(Day09Data)) // 20665830408335
fmt.Printf("Day 09. Basic: %d\n", day_09.SolveBasic(Day09Data)) //
case "09-complex":
fmt.Printf("Day 09. Complex: %d\n", day_09.SolveComplex(Day09Data)) // 20665830408335
fmt.Printf("Day 09. Complex: %d\n", day_09.SolveComplex(Day09Data)) //
case "10-basic":
fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) // 20665830408335
fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) //
case "10-complex":
fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) // 20665830408335
fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) //
}
}