-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New Exercise]: Killer Sudoku Helper (#284)
* [New Exercise]: Killer Sudoku Helper * Update test file -Fix some blank lines and update test file
- Loading branch information
Showing
7 changed files
with
336 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
63 changes: 63 additions & 0 deletions
63
exercises/practice/killer-sudoku-helper/.docs/instructions.md
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,63 @@ | ||
# Instructions | ||
|
||
A friend of yours is learning how to solve Killer Sudokus (rules below) but struggling to figure out which digits can go in a cage. | ||
They ask you to help them out by writing a small program that lists all valid combinations for a given cage, and any constraints that affect the cage. | ||
|
||
To make the output of your program easy to read, the combinations it returns must be sorted. | ||
|
||
## Killer Sudoku Rules | ||
|
||
- [Standard Sudoku rules][sudoku-rules] apply. | ||
- The digits in a cage, usually marked by a dotted line, add up to the small number given in the corner of the cage. | ||
- A digit may only occur once in a cage. | ||
|
||
For a more detailed explanation, check out [this guide][killer-guide]. | ||
|
||
## Example 1: Cage with only 1 possible combination | ||
|
||
In a 3-digit cage with a sum of 7, there is only one valid combination: 124. | ||
|
||
- 1 + 2 + 4 = 7 | ||
- Any other combination that adds up to 7, e.g. 232, would violate the rule of not repeating digits within a cage. | ||
|
||
![Sudoku grid, with three killer cages that are marked as grouped together. The first killer cage is in the 3×3 box in the top left corner of the grid. The middle column of that box forms the cage, with the followings cells from top to bottom: first cell contains a 1 and a pencil mark of 7, indicating a cage sum of 7, second cell contains a 2, third cell contains a 5. The numbers are highlighted in red to indicate a mistake. The second killer cage is in the central 3×3 box of the grid. The middle column of that box forms the cage, with the followings cells from top to bottom: first cell contains a 1 and a pencil mark of 7, indicating a cage sum of 7, second cell contains a 2, third cell contains a 4. None of the numbers in this cage are highlighted and therefore don't contain any mistakes. The third killer cage follows the outside corner of the central 3×3 box of the grid. It is made up of the following three cells: the top left cell of the cage contains a 2, highlighted in red, and a cage sum of 7. The top right cell of the cage contains a 3. The bottom right cell of the cage contains a 2, highlighted in red. All other cells are empty.][one-solution-img] | ||
|
||
## Example 2: Cage with several combinations | ||
|
||
In a 2-digit cage with a sum 10, there are 4 possible combinations: | ||
|
||
- 19 | ||
- 28 | ||
- 37 | ||
- 46 | ||
|
||
![Sudoku grid, all squares empty except for the middle column, column 5, which has 8 rows filled. Each continguous two rows form a killer cage and are marked as grouped together. From top to bottom: first group is a cell with value 1 and a pencil mark indicating a cage sum of 10, cell with value 9. Second group is a cell with value 2 and a pencil mark of 10, cell with value 8. Third group is a cell with value 3 and a pencil mark of 10, cell with value 7. Fourth group is a cell with value 4 and a pencil mark of 10, cell with value 6. The last cell in the column is empty.][four-solutions-img] | ||
|
||
## Example 3: Cage with several combinations that is restricted | ||
|
||
In a 2-digit cage with a sum 10, where the column already contains a 1 and a 4, there are 2 possible combinations: | ||
|
||
- 28 | ||
- 37 | ||
|
||
19 and 46 are not possible due to the 1 and 4 in the column according to standard Sudoku rules. | ||
|
||
![Sudoku grid, all squares empty except for the middle column, column 5, which has 8 rows filled. The first row contains a 4, the second is empty, and the third contains a 1. The 1 is highlighted in red to indicate a mistake. The last 6 rows in the column form killer cages of two cells each. From top to bottom: first group is a cell with value 2 and a pencil mark indicating a cage sum of 10, cell with value 8. Second group is a cell with value 3 and a pencil mark of 10, cell with value 7. Third group is a cell with value 1, highlighted in red, and a pencil mark of 10, cell with value 9.][not-possible-img] | ||
|
||
## Trying it yourself | ||
|
||
If you want to give an approachable Killer Sudoku a go, you can try out [this puzzle][clover-puzzle] by Clover, featured by [Mark Goodliffe on Cracking The Cryptic on the 21st of June 2021][goodliffe-video]. | ||
|
||
You can also find Killer Sudokus in varying difficulty in numerous newspapers, as well as Sudoku apps, books and websites. | ||
|
||
## Credit | ||
|
||
The screenshots above have been generated using [F-Puzzles.com](https://www.f-puzzles.com/), a Puzzle Setting Tool by Eric Fox. | ||
|
||
[sudoku-rules]: https://masteringsudoku.com/sudoku-rules-beginners/ | ||
[killer-guide]: https://masteringsudoku.com/killer-sudoku/ | ||
[one-solution-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example1.png | ||
[four-solutions-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example2.png | ||
[not-possible-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example3.png | ||
[clover-puzzle]: https://app.crackingthecryptic.com/sudoku/HqTBn3Pr6R | ||
[goodliffe-video]: https://youtu.be/c_NjEbFEeW0?t=1180 |
64 changes: 64 additions & 0 deletions
64
exercises/practice/killer-sudoku-helper/.meta/KillerSudokuHelper.example.ps1
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,64 @@ | ||
Function Invoke-KillerSudokuHelper() { | ||
<# | ||
.SYNOPSIS | ||
Implement a function to help solve killer sudoku. | ||
.DESCRIPTION | ||
Given a cage of certain size, a sum and an array of excluded number, follow the rules and find all possible combinations to fill the cage. | ||
To make the output of your program easy to read, the combinations it returns must be sorted. | ||
For example: | ||
7, [][][], No exclusion => Only possible solution is [1][2][4] | ||
[1][1][5], [1][3][3], [2][2][3] are all in violation of the "A digit may only occur once in a cage" rule. | ||
.PARAMETER Sum | ||
The target sum: total of all number in a cage. | ||
.PARAMETER Size | ||
The size of the cage: total cells available for the sum. | ||
.PARAMETER Exlude | ||
An array of number that should be excluded from consideration for the sum. | ||
.EXAMPLE | ||
Invoke-KillerSudokuHelper -Sum 5 -Size 2 -Exclude @(1) | ||
Return: @( ,@(2, 3)) | ||
#> | ||
[CmdletBinding()] | ||
Param( | ||
[int]$Sum, | ||
[int]$Size, | ||
[int[]]$Exclude | ||
) | ||
$potential = 1..9 | Where-Object {$_ -le $sum -and $Exclude -notcontains $_} | ||
|
||
$combinations = Get-Combination -Target $Sum -Size $Size -Range $potential -Result @() | ||
$combinations | ||
} | ||
|
||
function Get-Combination { | ||
<# | ||
.DESCRIPTION | ||
Recursive function to help fincing all posible solutions. | ||
#> | ||
param ( | ||
[int]$Target, | ||
[int]$Size, | ||
[int[]]$Range, | ||
[int[]]$Result | ||
) | ||
if ($Result.Count -eq $Size -and $Target -eq 0) { | ||
return (,$Result) | ||
} | ||
|
||
for ($i = 0; $i -lt $Range.Count; $i++) { | ||
$num = $Range[$i] | ||
if ($num -le $Target) { | ||
$newTarget = $Target - $num | ||
$newResult = $Result + $num | ||
|
||
$newRange = $Range[($i + 1)..($Range.Count - 1)] | ||
Get-Combination $newTarget $Size $newRange $newResult | ||
} | ||
} | ||
} |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"glaxxie" | ||
], | ||
"files": { | ||
"solution": [ | ||
"KillerSudokuHelper.ps1" | ||
], | ||
"test": [ | ||
"KillerSudokuHelper.tests.ps1" | ||
], | ||
"example": [ | ||
".meta/KillerSudokuHelper.example.ps1" | ||
] | ||
}, | ||
"blurb": "Write a tool that makes it easier to solve Killer Sudokus", | ||
"source": "Created by Sascha Mann, Jeremy Walker, and BethanyG for the Julia track on Exercism.", | ||
"source_url": "https://github.com/exercism/julia/pull/413" | ||
} |
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,49 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[2aaa8f13-11b5-4054-b95c-a906e4d79fb6] | ||
description = "Trivial 1-digit cages -> 1" | ||
|
||
[4645da19-9fdd-4087-a910-a6ed66823563] | ||
description = "Trivial 1-digit cages -> 2" | ||
|
||
[07cfc704-f8aa-41b2-8f9a-cbefb674cb48] | ||
description = "Trivial 1-digit cages -> 3" | ||
|
||
[22b8b2ba-c4fd-40b3-b1bf-40aa5e7b5f24] | ||
description = "Trivial 1-digit cages -> 4" | ||
|
||
[b75d16e2-ff9b-464d-8578-71f73094cea7] | ||
description = "Trivial 1-digit cages -> 5" | ||
|
||
[bcbf5afc-4c89-4ff6-9357-07ab4d42788f] | ||
description = "Trivial 1-digit cages -> 6" | ||
|
||
[511b3bf8-186f-4e35-844f-c804d86f4a7a] | ||
description = "Trivial 1-digit cages -> 7" | ||
|
||
[bd09a60d-3aca-43bd-b6aa-6ccad01bedda] | ||
description = "Trivial 1-digit cages -> 8" | ||
|
||
[9b539f27-44ea-4ff8-bd3d-c7e136bee677] | ||
description = "Trivial 1-digit cages -> 9" | ||
|
||
[0a8b2078-b3a4-4dbd-be0d-b180f503d5c3] | ||
description = "Cage with sum 45 contains all digits 1:9" | ||
|
||
[2635d7c9-c716-4da1-84f1-c96e03900142] | ||
description = "Cage with only 1 possible combination" | ||
|
||
[a5bde743-e3a2-4a0c-8aac-e64fceea4228] | ||
description = "Cage with several combinations" | ||
|
||
[dfbf411c-737d-465a-a873-ca556360c274] | ||
description = "Cage with several combinations that is restricted" |
35 changes: 35 additions & 0 deletions
35
exercises/practice/killer-sudoku-helper/KillerSudokuHelper.ps1
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,35 @@ | ||
Function Invoke-KillerSudokuHelper() { | ||
<# | ||
.SYNOPSIS | ||
Implement a function to help solve killer sudoku. | ||
.DESCRIPTION | ||
Given a cage of certain size, a sum and an array of excluded number, follow the rules and find all possible combinations to fill the cage. | ||
To make the output of your program easy to read, the combinations it returns must be sorted. | ||
For example: | ||
7, [][][], No exclusion => Only possible solution is [1][2][4] | ||
[1][1][5], [1][3][3], [2][2][3] are all in violation of the "A digit may only occur once in a cage" rule. | ||
.PARAMETER Sum | ||
The target sum: total of all number in a cage. | ||
.PARAMETER Size | ||
The size of the cage: total cells available for the sum. | ||
.PARAMETER Exlude | ||
An array of number that should be excluded from consideration for the sum. | ||
.EXAMPLE | ||
Invoke-KillerSudokuHelper -Sum 5 -Size 2 -Exclude @(1) | ||
Return: @( ,@(2, 3)) | ||
#> | ||
[CmdletBinding()] | ||
Param( | ||
[int]$Sum, | ||
[int]$Size, | ||
[int[]]$Exclude | ||
) | ||
|
||
Throw "Please implement this function" | ||
} |
98 changes: 98 additions & 0 deletions
98
exercises/practice/killer-sudoku-helper/KillerSudokuHelper.tests.ps1
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,98 @@ | ||
BeforeAll { | ||
. "./KillerSudokuHelper.ps1" | ||
} | ||
|
||
Describe "KillerSudokuHelper test cases" { | ||
Context "Trivial 1-digit cages" { | ||
It "Trivial 1-digit cages -> 1" { | ||
$got = Invoke-KillerSudokuHelper -Sum 1 -Size 1 -Exclude @() | ||
$want = @( @(1)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 2" { | ||
$got = Invoke-KillerSudokuHelper -Sum 2 -Size 1 -Exclude @() | ||
$want = @( @(2)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 3" { | ||
$got = Invoke-KillerSudokuHelper -Sum 3 -Size 1 -Exclude @() | ||
$want = @( @(3)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 4" { | ||
$got = Invoke-KillerSudokuHelper -Sum 4 -Size 1 -Exclude @() | ||
$want = @( @(4)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 5" { | ||
$got = Invoke-KillerSudokuHelper -Sum 5 -Size 1 -Exclude @() | ||
$want = @( @(5)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 6" { | ||
$got = Invoke-KillerSudokuHelper -Sum 6 -Size 1 -Exclude @() | ||
$want = @( @(6)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 7" { | ||
$got = Invoke-KillerSudokuHelper -Sum 7 -Size 1 -Exclude @() | ||
$want = @( @(7)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 8" { | ||
$got = Invoke-KillerSudokuHelper -Sum 8 -Size 1 -Exclude @() | ||
$want = @( @(8)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Trivial 1-digit cages -> 9" { | ||
$got = Invoke-KillerSudokuHelper -Sum 9 -Size 1 -Exclude @() | ||
$want = @( @(9)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
} | ||
|
||
It "Cage with sum 45 contains all digits 1:9" { | ||
$got = Invoke-KillerSudokuHelper -Sum 45 -Size 9 -Exclude @() | ||
$want = @( @(1, 2, 3, 4, 5, 6, 7, 8, 9)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Cage with only 1 possible combination" { | ||
$got = Invoke-KillerSudokuHelper -Sum 7 -Size 3 -Exclude @() | ||
$want = @( @(1, 2, 4)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Cage with several combinations" { | ||
$got = Invoke-KillerSudokuHelper -Sum 10 -Size 2 -Exclude @() | ||
$want = @( @(1, 9), @(2, 8), @(3, 7), @(4, 6)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
|
||
It "Cage with several combinations that is restricted" { | ||
$got = Invoke-KillerSudokuHelper -Sum 10 -Size 2 -Exclude @(1, 4) | ||
$want = @( @(2, 8), @(3, 7)) | ||
|
||
$got | Should -BeExactly $want | ||
} | ||
} |