Skip to content

Commit

Permalink
Merge pull request #500 from vaskoz/day239
Browse files Browse the repository at this point in the history
Day239
  • Loading branch information
vaskoz committed Apr 22, 2019
2 parents 41fc5da + f6f89a9 commit b9669ea
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ problems from
* [Day 236](https://github.com/vaskoz/dailycodingproblem-go/issues/485)
* [Day 237](https://github.com/vaskoz/dailycodingproblem-go/issues/487)
* [Day 238](https://github.com/vaskoz/dailycodingproblem-go/issues/489)
* [Day 239](https://github.com/vaskoz/dailycodingproblem-go/issues/491)
* [Day 240](https://github.com/vaskoz/dailycodingproblem-go/issues/492)
* [Day 241](https://github.com/vaskoz/dailycodingproblem-go/issues/493)
* [Day 242](https://github.com/vaskoz/dailycodingproblem-go/issues/498)
36 changes: 36 additions & 0 deletions day239/problem.go
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
}
16 changes: 16 additions & 0 deletions day239/problem_test.go
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()
}
}

0 comments on commit b9669ea

Please sign in to comment.