-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
day 245: refactor to account for impassable puzzles
- Loading branch information
Showing
2 changed files
with
33 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
package day245 | ||
|
||
import "errors" | ||
|
||
var errImpassable = errors.New("impossible to reach the end") | ||
|
||
// ErrImpassable returns the error associated with an impassable | ||
// route. | ||
func ErrImpassable() error { | ||
return errImpassable | ||
} | ||
|
||
// MinimumJumps returns the minimum steps to reach the end. | ||
// Each element contains the max steps that can be taken | ||
// in a single jump. | ||
func MinimumJumps(maxSteps []int) int { | ||
// Returns an error if impassable. | ||
func MinimumJumps(maxSteps []int) (int, error) { | ||
if len(maxSteps) < 2 { | ||
return 0 | ||
return 0, nil | ||
} | ||
minJumps := int(^uint(0) >> 1) | ||
if maxSteps[0] == 0 { // can't jump from this cell | ||
return minJumps | ||
return 0, errImpassable | ||
} | ||
impassable := true | ||
for jump := maxSteps[0]; jump > 0; jump-- { | ||
if jump < len(maxSteps) { | ||
if result := MinimumJumps(maxSteps[jump:]); result < minJumps { | ||
if result, impass := MinimumJumps(maxSteps[jump:]); impass == nil && result < minJumps { | ||
impassable = false | ||
minJumps = result | ||
} | ||
} | ||
} | ||
return minJumps + 1 | ||
if impassable { | ||
return 0, errImpassable | ||
} | ||
return minJumps + 1, 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