Skip to content

Commit c8ce079

Browse files
Add killer-sudoku-helper tests (#2381)
1 parent 4a4f7b0 commit c8ce079

File tree

11 files changed

+498
-0
lines changed

11 files changed

+498
-0
lines changed

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,19 @@
26622662
"while-loops"
26632663
],
26642664
"difficulty": 2
2665+
},
2666+
{
2667+
"slug": "killer-sudoku-helper",
2668+
"name": "Killer Sudoku Helper",
2669+
"uuid": "ef287b7e-50ca-4072-93e8-84efbbe77079",
2670+
"practices": [
2671+
"arrays"
2672+
],
2673+
"prerequisites": [
2674+
"arrays",
2675+
"numbers"
2676+
],
2677+
"difficulty": 7
26652678
}
26662679
],
26672680
"foregone": [

exercises/Exercises.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EliudsEggs", "practice\eliu
347347
EndProject
348348
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Knapsack", "practice\knapsack\Knapsack.csproj", "{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC}"
349349
EndProject
350+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KillerSudokuHelper", "practice\killer-sudoku-helper\KillerSudokuHelper.csproj", "{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}"
351+
EndProject
350352
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquareRoot", "practice\square-root\SquareRoot.csproj", "{5C05051E-D46C-4544-9CF8-F2A748F63172}"
351353
EndProject
352354
Global
@@ -1039,6 +1041,10 @@ Global
10391041
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Debug|Any CPU.Build.0 = Debug|Any CPU
10401042
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Release|Any CPU.ActiveCfg = Release|Any CPU
10411043
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Release|Any CPU.Build.0 = Release|Any CPU
1044+
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1045+
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
1046+
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
1047+
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.Build.0 = Release|Any CPU
10421048
EndGlobalSection
10431049
GlobalSection(SolutionProperties) = preSolution
10441050
HideSolutionNode = FALSE
@@ -1215,6 +1221,7 @@ Global
12151221
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12161222
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12171223
{5C05051E-D46C-4544-9CF8-F2A748F63172} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
1224+
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12181225
EndGlobalSection
12191226
GlobalSection(ExtensibilityGlobals) = postSolution
12201227
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Instructions
2+
3+
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.
4+
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.
5+
6+
To make the output of your program easy to read, the combinations it returns must be sorted.
7+
8+
## Killer Sudoku Rules
9+
10+
- [Standard Sudoku rules][sudoku-rules] apply.
11+
- The digits in a cage, usually marked by a dotted line, add up to the small number given in the corner of the cage.
12+
- A digit may only occur once in a cage.
13+
14+
For a more detailed explanation, check out [this guide][killer-guide].
15+
16+
## Example 1: Cage with only 1 possible combination
17+
18+
In a 3-digit cage with a sum of 7, there is only one valid combination: 124.
19+
20+
- 1 + 2 + 4 = 7
21+
- Any other combination that adds up to 7, e.g. 232, would violate the rule of not repeating digits within a cage.
22+
23+
![Sudoku grid, with three killer cages that are marked as grouped together.
24+
The first killer cage is in the 3×3 box in the top left corner of the grid.
25+
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.
26+
The numbers are highlighted in red to indicate a mistake.
27+
The second killer cage is in the central 3×3 box of the grid.
28+
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.
29+
None of the numbers in this cage are highlighted and therefore don't contain any mistakes.
30+
The third killer cage follows the outside corner of the central 3×3 box of the grid.
31+
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.
32+
The top right cell of the cage contains a 3.
33+
The bottom right cell of the cage contains a 2, highlighted in red. All other cells are empty.][one-solution-img]
34+
35+
## Example 2: Cage with several combinations
36+
37+
In a 2-digit cage with a sum 10, there are 4 possible combinations:
38+
39+
- 19
40+
- 28
41+
- 37
42+
- 46
43+
44+
![Sudoku grid, all squares empty except for the middle column, column 5, which has 8 rows filled.
45+
Each continguous two rows form a killer cage and are marked as grouped together.
46+
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.
47+
Second group is a cell with value 2 and a pencil mark of 10, cell with value 8.
48+
Third group is a cell with value 3 and a pencil mark of 10, cell with value 7.
49+
Fourth group is a cell with value 4 and a pencil mark of 10, cell with value 6.
50+
The last cell in the column is empty.][four-solutions-img]
51+
52+
## Example 3: Cage with several combinations that is restricted
53+
54+
In a 2-digit cage with a sum 10, where the column already contains a 1 and a 4, there are 2 possible combinations:
55+
56+
- 28
57+
- 37
58+
59+
19 and 46 are not possible due to the 1 and 4 in the column according to standard Sudoku rules.
60+
61+
![Sudoku grid, all squares empty except for the middle column, column 5, which has 8 rows filled.
62+
The first row contains a 4, the second is empty, and the third contains a 1.
63+
The 1 is highlighted in red to indicate a mistake.
64+
The last 6 rows in the column form killer cages of two cells each.
65+
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.
66+
Second group is a cell with value 3 and a pencil mark of 10, cell with value 7.
67+
Third group is a cell with value 1, highlighted in red, and a pencil mark of 10, cell with value 9.][not-possible-img]
68+
69+
## Trying it yourself
70+
71+
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].
72+
73+
You can also find Killer Sudokus in varying difficulty in numerous newspapers, as well as Sudoku apps, books and websites.
74+
75+
## Credit
76+
77+
The screenshots above have been generated using [F-Puzzles.com](https://www.f-puzzles.com/), a Puzzle Setting Tool by Eric Fox.
78+
79+
[sudoku-rules]: https://masteringsudoku.com/sudoku-rules-beginners/
80+
[killer-guide]: https://masteringsudoku.com/killer-sudoku/
81+
[one-solution-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example1.png
82+
[four-solutions-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example2.png
83+
[not-possible-img]: https://assets.exercism.org/images/exercises/killer-sudoku-helper/example3.png
84+
[clover-puzzle]: https://app.crackingthecryptic.com/sudoku/HqTBn3Pr6R
85+
[goodliffe-video]: https://youtu.be/c_NjEbFEeW0?t=1180
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
###############################
2+
# Core EditorConfig Options #
3+
###############################
4+
5+
; This file is for unifying the coding style for different editors and IDEs.
6+
; More information at:
7+
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
8+
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
9+
10+
root = true
11+
12+
[*]
13+
indent_style = space
14+
15+
[KillerSudokuHelper.cs]
16+
indent_size = 4
17+
18+
###############################
19+
# .NET Coding Conventions #
20+
###############################
21+
22+
# Organize usings
23+
dotnet_sort_system_directives_first = true
24+
dotnet_separate_import_directive_groups = true
25+
26+
# this. preferences
27+
dotnet_style_qualification_for_field = false:suggestion
28+
dotnet_style_qualification_for_property = false:suggestion
29+
dotnet_style_qualification_for_method = false:suggestion
30+
dotnet_style_qualification_for_event = false:suggestion
31+
32+
# Language keywords vs BCL types preferences
33+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
34+
dotnet_style_predefined_type_for_member_access = true:suggestion
35+
36+
# Parentheses preferences
37+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
38+
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
39+
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
40+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
41+
42+
# Modifier preferences
43+
dotnet_style_require_accessibility_modifiers = always:suggestion
44+
dotnet_style_readonly_field = true:suggestion
45+
46+
# Expression-level preferences
47+
dotnet_style_object_initializer = true:suggestion
48+
dotnet_style_collection_initializer = true:suggestion
49+
dotnet_style_explicit_tuple_names = true:suggestion
50+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
51+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
52+
dotnet_style_prefer_auto_properties = true:suggestion
53+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
54+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
55+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
56+
dotnet_style_coalesce_expression = true:suggestion
57+
dotnet_style_null_propagation = true:suggestion
58+
59+
###############################
60+
# Naming Conventions #
61+
###############################
62+
63+
# Style Definitions
64+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
65+
66+
# Use PascalCase for constant fields
67+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
68+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
69+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
70+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
71+
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
72+
dotnet_naming_symbols.constant_fields.required_modifiers = const
73+
74+
###############################
75+
# C# Code Style Rules #
76+
###############################
77+
78+
# var preferences
79+
csharp_style_var_for_built_in_types = true:none
80+
csharp_style_var_when_type_is_apparent = true:none
81+
csharp_style_var_elsewhere = true:none
82+
83+
# Expression-bodied members
84+
csharp_style_expression_bodied_methods = true:suggestion
85+
csharp_style_expression_bodied_constructors = true:suggestion
86+
csharp_style_expression_bodied_operators = true:suggestion
87+
csharp_style_expression_bodied_properties = true:suggestion
88+
csharp_style_expression_bodied_indexers = true:suggestion
89+
csharp_style_expression_bodied_accessors = true:suggestion
90+
91+
# Pattern-matching preferences
92+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
93+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
94+
95+
# Null-checking preferences
96+
csharp_style_throw_expression = true:suggestion
97+
csharp_style_conditional_delegate_call = true:suggestion
98+
99+
# Modifier preferences
100+
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
101+
102+
# Expression-level preferences
103+
csharp_prefer_braces = true:none
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
csharp_style_deconstructed_variable_declaration = true:suggestion
106+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
107+
csharp_style_inlined_variable_declaration = true:suggestion
108+
109+
###############################
110+
# C# Formatting Rules #
111+
###############################
112+
113+
# New line preferences
114+
csharp_new_line_before_open_brace = all
115+
csharp_new_line_before_else = true
116+
csharp_new_line_before_catch = true
117+
csharp_new_line_before_finally = true
118+
csharp_new_line_before_members_in_object_initializers = false
119+
csharp_new_line_before_members_in_anonymous_types = false
120+
csharp_new_line_between_query_expression_clauses = true
121+
122+
# Indentation preferences
123+
csharp_indent_case_contents = true
124+
csharp_indent_switch_labels = true
125+
csharp_indent_labels = flush_left
126+
127+
# Space preferences
128+
csharp_space_after_cast = false
129+
csharp_space_after_keywords_in_control_flow_statements = true
130+
csharp_space_between_method_declaration_parameter_list_parentheses = false
131+
csharp_space_between_method_call_parameter_list_parentheses = false
132+
csharp_space_before_colon_in_inheritance_clause = true
133+
csharp_space_after_colon_in_inheritance_clause = true
134+
csharp_space_around_binary_operators = before_and_after
135+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
136+
csharp_space_between_method_call_name_and_opening_parenthesis = false
137+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
138+
139+
# Wrapping preferences
140+
csharp_preserve_single_line_blocks = true
141+
csharp_preserve_single_line_statements = true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
public static class KillerSudokuHelper
5+
{
6+
private static readonly int[] Digits = [1, 2, 3, 4, 5, 6, 7, 8, 9];
7+
8+
public static IEnumerable<int[]> Combinations(int sum, int size, int[] exclude) =>
9+
Digits.Except(exclude).ToArray()
10+
.Combinations(size)
11+
.Where(comb => comb.Sum() == sum);
12+
13+
private static IEnumerable<T[]> Combinations<T>(this T[] items, int size)
14+
{
15+
if (size > items.Length) yield break;
16+
17+
var pool = items.ToArray();
18+
var indices = Enumerable.Range(0, size).ToArray();
19+
20+
yield return indices.Select(x => pool[x]).ToArray();
21+
22+
while (true)
23+
{
24+
var i = indices.Length - 1;
25+
while (i >= 0 && indices[i] == i + items.Length - size)
26+
i -= 1;
27+
28+
if (i < 0) yield break;
29+
30+
indices[i] += 1;
31+
32+
for (var j = i + 1; j < size; j += 1)
33+
indices[j] = indices[j - 1] + 1;
34+
35+
yield return indices.Select(x => pool[x]).ToArray();
36+
}
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Xunit;
2+
3+
public class {{ testClass }}
4+
{
5+
{{- for test in tests }}
6+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
7+
public void {{ test.testMethod }}()
8+
{
9+
{{- if test.expected | array.size > 1 }}
10+
int[][] expected = [
11+
{{- for row in test.expected }}
12+
{{ row }}{{- if !for.last }},{{ end -}}
13+
{{ end }}
14+
];
15+
{{- else }}
16+
int[][] expected = {{ test.expected }};
17+
{{ end -}}
18+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.cage.sum }}, {{ test.input.cage.size }}, {{ test.input.cage.exclude }}));
19+
}
20+
{{ end -}}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"KillerSudokuHelper.cs"
8+
],
9+
"test": [
10+
"KillerSudokuHelperTests.cs"
11+
],
12+
"example": [
13+
".meta/Example.cs"
14+
]
15+
},
16+
"blurb": "Write a tool that makes it easier to solve Killer Sudokus",
17+
"source": "Created by Sascha Mann, Jeremy Walker, and BethanyG for the Julia track on Exercism.",
18+
"source_url": "https://github.com/exercism/julia/pull/413"
19+
}

0 commit comments

Comments
 (0)