-
Notifications
You must be signed in to change notification settings - Fork 31
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
236 additions
and
0 deletions.
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
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,90 @@ | ||
package day427 | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// Elevation is a map of locations to their elevations. | ||
type Elevation map[int]int | ||
|
||
// PathDistance represents the distance for a particular path. | ||
type PathDistance map[int]map[int]int | ||
|
||
// MaxInt is the largest int. | ||
const MaxInt = int(^uint(0) >> 1) | ||
|
||
var errNotPossible = errors.New("no path possible") | ||
|
||
// ErrNotPossible is the error returned if a path doesn't exist. | ||
func ErrNotPossible() error { | ||
return errNotPossible | ||
} | ||
|
||
// ShortestRunnerPath with the condition that the route goes entirely | ||
// uphill at first, and then entirely downhill. | ||
// Always starts at location 0 and end at location 0. | ||
func ShortestRunnerPath(elevations Elevation, paths PathDistance) (int, error) { | ||
min := MaxInt | ||
startElev := elevations[0] | ||
|
||
for next, distance := range paths[0] { | ||
nextElev := elevations[next] | ||
if nextElev > startElev { | ||
dist, err := shortestRunnerPath(elevations, paths, map[int]struct{}{}, true, next) | ||
if err == nil && dist+distance < min { | ||
min = dist + distance | ||
} | ||
} | ||
} | ||
|
||
if min == MaxInt { | ||
return 0, ErrNotPossible() | ||
} | ||
|
||
return min, nil | ||
} | ||
|
||
func shortestRunnerPath( | ||
elevations Elevation, | ||
paths PathDistance, | ||
visited map[int]struct{}, | ||
up bool, | ||
n int) (int, error) { | ||
if n == 0 { | ||
return 0, nil | ||
} | ||
|
||
min := MaxInt | ||
currentElevation := elevations[n] | ||
|
||
for next, distance := range paths[n] { | ||
if _, visit := visited[next]; !visit { | ||
nextElevation := elevations[next] | ||
if up && nextElevation > currentElevation { | ||
visited[next] = struct{}{} | ||
dist, err := shortestRunnerPath(elevations, paths, visited, true, next) | ||
totalDist := dist + distance | ||
|
||
if err == nil && totalDist < min { | ||
min = totalDist | ||
} | ||
|
||
delete(visited, next) | ||
} else if nextElevation < currentElevation { | ||
visited[next] = struct{}{} | ||
dist, err := shortestRunnerPath(elevations, paths, visited, false, next) | ||
totalDist := dist + distance | ||
if err == nil && totalDist < min { | ||
min = totalDist | ||
} | ||
delete(visited, next) | ||
} | ||
} | ||
} | ||
|
||
if min == MaxInt { | ||
return 0, ErrNotPossible() | ||
} | ||
|
||
return min, nil | ||
} |
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,145 @@ | ||
package day427 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
elevations Elevation | ||
paths PathDistance | ||
shortestRoute int | ||
err error | ||
}{ | ||
{ | ||
Elevation{ | ||
0: 15, | ||
1: 10, | ||
2: 25, | ||
}, | ||
PathDistance{ | ||
0: { | ||
2: 5, | ||
}, | ||
1: { | ||
0: 1, | ||
}, | ||
2: { | ||
1: 7, | ||
}, | ||
}, | ||
0, | ||
ErrNotPossible(), | ||
}, | ||
{ | ||
Elevation{ | ||
0: 10, | ||
1: 25, | ||
2: 15, | ||
}, | ||
PathDistance{ | ||
0: { | ||
1: 15, | ||
2: 5, | ||
}, | ||
1: { | ||
0: 1, | ||
}, | ||
2: { | ||
1: 7, | ||
0: 30, | ||
}, | ||
}, | ||
13, | ||
nil, | ||
}, | ||
{ | ||
Elevation{ | ||
0: 10, | ||
1: 5, | ||
2: 7, | ||
}, | ||
PathDistance{ | ||
0: { | ||
1: 5, | ||
2: 10, | ||
}, | ||
1: { | ||
0: 5, | ||
}, | ||
2: { | ||
0: 10, | ||
}, | ||
}, | ||
0, | ||
ErrNotPossible(), | ||
}, | ||
{ | ||
Elevation{ | ||
0: 10, | ||
1: 5, | ||
2: 15, | ||
}, | ||
PathDistance{ | ||
0: { | ||
1: 5, | ||
2: 10, | ||
}, | ||
1: { | ||
0: 5, | ||
}, | ||
2: { | ||
0: 10, | ||
}, | ||
}, | ||
20, | ||
nil, | ||
}, | ||
{ | ||
Elevation{ | ||
0: 5, | ||
1: 25, | ||
2: 15, | ||
3: 20, | ||
4: 10, | ||
}, | ||
PathDistance{ | ||
0: { | ||
1: 10, | ||
2: 8, | ||
3: 15, | ||
}, | ||
1: { | ||
3: 12, | ||
}, | ||
2: { | ||
4: 10, | ||
}, | ||
3: { | ||
4: 5, | ||
0: 17, | ||
}, | ||
4: { | ||
0: 10, | ||
}, | ||
}, | ||
28, | ||
nil, | ||
}, | ||
} | ||
|
||
func TestShortestRunnerPath(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if result, err := ShortestRunnerPath(tc.elevations, tc.paths); result != tc.shortestRoute || err != tc.err { | ||
t.Errorf("Expected (%v,%v), got (%v,%v)", tc.shortestRoute, tc.err, result, err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkShortestRunnerPath(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ShortestRunnerPath(tc.elevations, tc.paths) // nolint | ||
} | ||
} | ||
} |