Day 10. part 2.
This commit is contained in:
parent
778ed08680
commit
2fdf520e2b
|
@ -0,0 +1,6 @@
|
||||||
|
012345
|
||||||
|
123456
|
||||||
|
234567
|
||||||
|
345678
|
||||||
|
4.6789
|
||||||
|
56789.
|
|
@ -41,7 +41,17 @@ func SolveBasic(input string) int {
|
||||||
|
|
||||||
// task:https://adventofcode.com/2024/day/10#part2
|
// task:https://adventofcode.com/2024/day/10#part2
|
||||||
func SolveComplex(input string) int {
|
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 {
|
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)
|
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 {
|
func loadMap(input string) TopographicMap {
|
||||||
input = strings.ReplaceAll(input, "\r", "")
|
input = strings.ReplaceAll(input, "\r", "")
|
||||||
topographicMap := TopographicMap{}
|
topographicMap := TopographicMap{}
|
||||||
|
|
|
@ -18,6 +18,9 @@ var ExampleData02 string
|
||||||
//go:embed example_data_03.txt
|
//go:embed example_data_03.txt
|
||||||
var ExampleData03 string
|
var ExampleData03 string
|
||||||
|
|
||||||
|
//go:embed example_data_04.txt
|
||||||
|
var ExampleData04 string
|
||||||
|
|
||||||
func TestBasicSolutionExample(t *testing.T) {
|
func TestBasicSolutionExample(t *testing.T) {
|
||||||
result := day_10.SolveBasic(ExampleData)
|
result := day_10.SolveBasic(ExampleData)
|
||||||
if result != 36 {
|
if result != 36 {
|
||||||
|
@ -48,7 +51,21 @@ func TestBasicSolutionExample03(t *testing.T) {
|
||||||
|
|
||||||
func TestComplexSolutionExample(t *testing.T) {
|
func TestComplexSolutionExample(t *testing.T) {
|
||||||
result := day_10.SolveComplex(ExampleData)
|
result := day_10.SolveComplex(ExampleData)
|
||||||
if result != 2858 {
|
if result != 81 {
|
||||||
t.Fatalf("Expected 2858 received %d", result)
|
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
16
main.go
|
@ -45,21 +45,21 @@ func main() {
|
||||||
case "06-complex":
|
case "06-complex":
|
||||||
fmt.Printf("Day 06. Complex: %d\n", day_06.SolveComplex(Day06Data)) //
|
fmt.Printf("Day 06. Complex: %d\n", day_06.SolveComplex(Day06Data)) //
|
||||||
case "07-basic":
|
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":
|
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":
|
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":
|
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":
|
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":
|
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":
|
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":
|
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)) //
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue