-
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.
Merge pull request #500 from vaskoz/day239
Day239
- Loading branch information
Showing
3 changed files
with
53 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,36 @@ | ||
package day239 | ||
|
||
// ValidUnlockKeypadNumber returns the number of possible | ||
// valid combinations. | ||
func ValidUnlockKeypadNumber() int { | ||
var jumps [10][10]int | ||
jumps[1][3], jumps[3][1] = 2, 2 | ||
jumps[7][9], jumps[9][7] = 8, 8 | ||
jumps[1][7], jumps[7][1] = 4, 4 | ||
jumps[3][9], jumps[9][3] = 6, 6 | ||
jumps[1][9], jumps[9][1], jumps[2][8], jumps[8][2], | ||
jumps[3][7], jumps[7][3], jumps[4][6], jumps[6][4] = 5, 5, 5, 5, 5, 5, 5, 5 | ||
var visited [10]bool | ||
ways := 0 | ||
for numDots := 4; numDots < 10; numDots++ { | ||
ways += 4 * validUnlockKeypadNumber(visited, jumps, 1, numDots-1) | ||
ways += 4 * validUnlockKeypadNumber(visited, jumps, 2, numDots-1) | ||
ways += validUnlockKeypadNumber(visited, jumps, 5, numDots-1) | ||
} | ||
return ways | ||
} | ||
|
||
func validUnlockKeypadNumber(visited [10]bool, jumps [10][10]int, cur, to int) int { | ||
if to == 0 { | ||
return 1 | ||
} | ||
ways := 0 | ||
visited[cur] = true | ||
for i := 1; i < 10; i++ { | ||
if !visited[i] && (jumps[i][cur] == 0 || visited[jumps[i][cur]]) { | ||
ways += validUnlockKeypadNumber(visited, jumps, i, to-1) | ||
} | ||
} | ||
visited[cur] = false | ||
return ways | ||
} |
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,16 @@ | ||
package day239 | ||
|
||
import "testing" | ||
|
||
func TestValidUnlockKeypadNumber(t *testing.T) { | ||
t.Parallel() | ||
if result := ValidUnlockKeypadNumber(); result != 389112 { | ||
t.Errorf("Expected 389112, got %v", result) | ||
} | ||
} | ||
|
||
func BenchmarkValidUnlockKeypadNumber(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
ValidUnlockKeypadNumber() | ||
} | ||
} |