diff --git a/day_10/example_data_04.txt b/day_10/example_data_04.txt new file mode 100644 index 0000000..b70d636 --- /dev/null +++ b/day_10/example_data_04.txt @@ -0,0 +1,6 @@ +012345 +123456 +234567 +345678 +4.6789 +56789. \ No newline at end of file diff --git a/day_10/solution.go b/day_10/solution.go index b507c17..650307b 100644 --- a/day_10/solution.go +++ b/day_10/solution.go @@ -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{} diff --git a/day_10/solution_test.go b/day_10/solution_test.go index 6332d50..daabdae 100644 --- a/day_10/solution_test.go +++ b/day_10/solution_test.go @@ -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) } } diff --git a/main.go b/main.go index 24b1950..e18bd02 100644 --- a/main.go +++ b/main.go @@ -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)) // } }