From 0fcec8efd951b1ba59a105a788077e4c56a63a5e Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 21 Apr 2019 16:57:36 -0600 Subject: [PATCH 1/5] day 239: unlock patterns for android keypad --- day239/problem.go | 34 ++++++++++++++++++++++++++++++++++ day239/problem_test.go | 16 ++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 day239/problem.go create mode 100644 day239/problem_test.go diff --git a/day239/problem.go b/day239/problem.go new file mode 100644 index 0000000..80f37b6 --- /dev/null +++ b/day239/problem.go @@ -0,0 +1,34 @@ +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 i := 1; i < 10; i++ { + ways += 4 * validUnlockKeypadNumber(visited, jumps, 1, i-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 +} diff --git a/day239/problem_test.go b/day239/problem_test.go new file mode 100644 index 0000000..38c2c83 --- /dev/null +++ b/day239/problem_test.go @@ -0,0 +1,16 @@ +package day239 + +import "testing" + +func TestValidUnlockKeypadNumber(t *testing.T) { + t.Parallel() + if result := ValidUnlockKeypadNumber(); result != 152316 { + t.Errorf("Expected 152316, got %v", result) + } +} + +func BenchmarkValidUnlockKeypadNumber(b *testing.B) { + for i := 0; i < b.N; i++ { + ValidUnlockKeypadNumber() + } +} From 501c02d36a4b11bfb01702c288cc135f85ad5a5a Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 21 Apr 2019 16:58:23 -0600 Subject: [PATCH 2/5] add day 239 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d42c62a..4f7bd49 100644 --- a/README.md +++ b/README.md @@ -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) From 5f812f94b336340820f05a904da419049c341ea2 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 21 Apr 2019 17:04:24 -0600 Subject: [PATCH 3/5] day 239: at least four dots. start from 1, 2, 5 only --- day239/problem.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/day239/problem.go b/day239/problem.go index 80f37b6..3603765 100644 --- a/day239/problem.go +++ b/day239/problem.go @@ -12,8 +12,10 @@ func ValidUnlockKeypadNumber() int { 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 i := 1; i < 10; i++ { + for i := 4; i < 10; i++ { ways += 4 * validUnlockKeypadNumber(visited, jumps, 1, i-1) + ways += 4 * validUnlockKeypadNumber(visited, jumps, 2, i-1) + ways += validUnlockKeypadNumber(visited, jumps, 5, i-1) } return ways } From ca148bae9e51968e5420c3b5dc023e195a7a547e Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 21 Apr 2019 18:24:31 -0600 Subject: [PATCH 4/5] day 239: confirmed answer is 389,112 valid patterns --- day239/problem_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/day239/problem_test.go b/day239/problem_test.go index 38c2c83..4a0365f 100644 --- a/day239/problem_test.go +++ b/day239/problem_test.go @@ -4,8 +4,8 @@ import "testing" func TestValidUnlockKeypadNumber(t *testing.T) { t.Parallel() - if result := ValidUnlockKeypadNumber(); result != 152316 { - t.Errorf("Expected 152316, got %v", result) + if result := ValidUnlockKeypadNumber(); result != 389112 { + t.Errorf("Expected 389112, got %v", result) } } From f6f89a9588dce8cec13ec3c4efe282a98c95735f Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 21 Apr 2019 18:35:31 -0600 Subject: [PATCH 5/5] day 239: number of dots in the loop --- day239/problem.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/day239/problem.go b/day239/problem.go index 3603765..e471b02 100644 --- a/day239/problem.go +++ b/day239/problem.go @@ -12,10 +12,10 @@ func ValidUnlockKeypadNumber() int { 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 i := 4; i < 10; i++ { - ways += 4 * validUnlockKeypadNumber(visited, jumps, 1, i-1) - ways += 4 * validUnlockKeypadNumber(visited, jumps, 2, i-1) - ways += validUnlockKeypadNumber(visited, jumps, 5, i-1) + 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 }