Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fillFirstEmptyPosition-Implementierung #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/SudokuSolver/AlgorithmusBeschreibung.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Kernst�ck der L�sung ist eine rekursive Methode die ihre Endbedingung erreicht
sobald die Matrix vollst�ndig ausgef�llt ist und keine Regelverst��e enth�lt.
Die Methode �bernimmt eine Matrix und ersetzt die erste freie Position durch alle m�glichen Zaheln (1-9).
Aus den dadurch entstanden neuen Matrizen werden die gefiltert, die gegen die Regeln versto�en.
Die �brigen Matrizen gehen in das n�chste Rekursionslevel um die n�chste freie Position auszuf�llen.
Die "Root"-Methode liefert eine Liste aller m�glichen L�sungen zur�ck.
129 changes: 95 additions & 34 deletions Source/SudokuSolver/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,39 @@ let rec transpose = function
| (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
| _ -> []

let removeElements l count =
let rec r = fun counter list ->
if counter = count then
list
else
list |> List.tail |> r (counter + 1)
r 0 l

let rec buildTuples (l : int list list) =
if l.Length = 0 then
[]
else
let f = List.zip3 (List.head l) (List.nth l 1) (List.nth l 2)
List.append f (buildTuples (removeElements l 3))

let buildListFromTupel (a, b, c) =
[a ; b; c]

let rec combineTuples (l : (int * int * int) list) =
if l.Length = 0 then
[]
else
let f = List.head l
let s = List.nth l 1
let t = List.nth l 2
(buildListFromTupel f @ buildListFromTupel s @ buildListFromTupel t) :: combineTuples(removeElements l 3)

let GetRows = id
let GetCols = transpose
let GetBoxes = id // Homework
let GetBoxes l =
l
|> buildTuples
|> combineTuples

let isAllUnique numbers =
let set = new HashSet<_>()
Expand All @@ -22,42 +52,73 @@ let isMatrixValid matrix =
rows @ cols @ boxes
|> Seq.forall isAllUnique

let listContainsZero = List.forall ((<>) 0)
let listContainsZero = List.exists ((=) 0)

let isComplete matrix =
matrix
|> Seq.exists listContainsZero

let fillFirstEmptyPosition matrix = [matrix] // TODO: Homework
|> not

let rec fillFirstEmptyPositionWith matrix number =
let m = ref number
let replaceZero z =
match z with
| 0 ->
let g = !m
m := 0
g
| f -> id f
let changeList = List.map replaceZero
List.map changeList matrix

let fillFirstEmptyPosition (matrix : int list list) =
let replaceFunction = fillFirstEmptyPositionWith matrix
[ 1 .. 9 ]
|> List.map replaceFunction

let rec solve matrix =
if isComplete matrix then [matrix] else
matrix
|> fillFirstEmptyPosition
|> List.filter isMatrixValid
|> List.map solve
|> List.concat
























if isComplete matrix then [matrix]
else
matrix
|> fillFirstEmptyPosition
|> List.filter isMatrixValid
|> List.map solve
|> List.concat

let testData =
[[0; 0; 8; 3; 0; 0; 6; 0; 0]
[0; 0; 4; 0; 0; 0; 0; 1; 0]
[6; 7; 0; 0; 8; 0; 0; 0; 0]

[0; 1; 6; 4; 3; 0; 0; 0; 0]
[0; 0; 0; 7; 9; 0; 0; 2; 0]
[0; 9; 0; 0; 0; 0; 4; 0; 1]

[0; 0; 0; 9; 1; 0; 0; 0; 5]
[0; 0; 3; 0; 5; 0; 0; 0; 2]
[0; 5; 0; 0; 0; 0; 0; 7; 4]]

let printCell i cell =
printf "%A " cell
if (i % 3) = 2 then printf " "

let printRow i row =
Seq.iteri printCell row

printfn ""
if (i % 3) = 2 then printfn ""

let printGrid grid =
grid
|> Seq.iteri printRow
printfn ""

let printData l =
l
|> Seq.iter printGrid

testData
|> solve
|> printData

System.Console.ReadKey() |> ignore
15 changes: 14 additions & 1 deletion Source/SudokuSolver/SampleData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ let field2 =

[0; 0; 0; 9; 1; 0; 0; 0; 5]
[0; 0; 3; 0; 5; 0; 0; 0; 2]
[0; 5; 0; 0; 0; 0; 0; 7; 4]]
[0; 5; 0; 0; 0; 0; 0; 7; 4]]

let field3 =
[[6; 5; 0; 0; 7; 1; 0; 0; 4]
[7; 9; 8; 2; 0; 4; 0; 0; 6]
[0; 0; 4; 6; 0; 0; 3; 0; 0]

[0; 0; 0; 0; 2; 0; 8; 0; 0]
[1; 0; 2; 0; 4; 0; 6; 0; 5]
[0; 0; 9; 0; 1; 0; 0; 0; 0]

[0; 0; 6; 0; 0; 2; 1; 0; 0]
[9; 0; 0; 3; 0; 5; 7; 8; 2]
[8; 0; 0; 1; 9; 0; 0; 6; 3]]
7 changes: 7 additions & 0 deletions Source/SudokuSolver/SudokuSolver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "SudokuSolver", "SudokuSolve
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{48312B1C-323F-41DF-9D51-5F5C06E6F34B}"
ProjectSection(SolutionItems) = preProject
AlgorithmusBeschreibung.txt = AlgorithmusBeschreibung.txt
..\..\README.markdown = ..\..\README.markdown
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SudokuSolverCSharp", "..\SudokuSolverCSharp\SudokuSolverCSharp.csproj", "{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand All @@ -18,6 +21,10 @@ Global
{F37F8189-F2BC-415D-99DE-E9F692B5AA70}.Debug|x86.Build.0 = Debug|x86
{F37F8189-F2BC-415D-99DE-E9F692B5AA70}.Release|x86.ActiveCfg = Release|x86
{F37F8189-F2BC-415D-99DE-E9F692B5AA70}.Release|x86.Build.0 = Release|x86
{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}.Debug|x86.ActiveCfg = Debug|x86
{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}.Debug|x86.Build.0 = Debug|x86
{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}.Release|x86.ActiveCfg = Release|x86
{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
52 changes: 52 additions & 0 deletions Source/SudokuSolverCSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace SudokuSolverCSharp
{
class Program
{
private static readonly List<List<int>> Original = new List<List<int>>
{
new List<int>{1,5,9,13},
new List<int>{2,6,10,14},
new List<int>{3,7,11,15},
new List<int>{4,8,12,16}
};

private static readonly List<List<int>> Rsesult = new List<List<int>>
{
new List<int>{1,2,3,4},
new List<int>{5,6,7,8},
new List<int>{9,10,11,12},
new List<int>{13,14,15,16}
};

static void Main(string[] args)
{
PrintResult(Transpose(Original));
Console.ReadKey();
}

private static List<List<int>> Transpose(List<List<int>> original)
{
List<List<int>> result = new List<List<int>>();

for (int i = 0; i < original[0].Count; i++)
{
result.Add(new List<int>());
foreach (List<int> number in original)
{
result[i].Add(number[i]);
}
}

return result;
}

private static void PrintResult(List<List<int>> data)
{
data.ForEach(l => Console.WriteLine(string.Join("; ", l.Select(z => z.ToString()))));
}
}
}
36 changes: 36 additions & 0 deletions Source/SudokuSolverCSharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SudokuSolverCSharp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("SudokuSolverCSharp")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("558dd987-cc07-4d7e-a2d5-55f10507f706")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
57 changes: 57 additions & 0 deletions Source/SudokuSolverCSharp/SudokuSolverCSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0F2E3C11-483C-43E9-9645-3A4A34FCDDAA}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SudokuSolverCSharp</RootNamespace>
<AssemblyName>SudokuSolverCSharp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>