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

Kropki helper constraint #70

Open
wants to merge 2 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
36 changes: 36 additions & 0 deletions SudokuSolver/Constraints/AnykropkiConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static SudokuSolver.SolverUtility;
using System.IO;

namespace SudokuSolver.Constraints
{
[Constraint(DisplayName = "Anykropki", ConsoleName = "anykropki")]
public class AnykropkiConstraint : OrthogonalValueConstraint
{
public AnykropkiConstraint(Solver sudokuSolver, string options) : base(sudokuSolver, options)
{
}

protected override bool IsPairAllowedAcrossMarker(int markerValue, int v0, int v1)
{
switch (markerValue)
{
case 1:
return v0 + 1 == v1 || v1 + 1 == v0 || v0 * 2 == v1 || v1 * 2 == v0;
case 2:
return v0 * 2 != v1 && v1 * 2 != v0;
case 3:
return v0 + 1 != v1 && v1 + 1 != v0;
}
return true;
}

protected override IEnumerable<OrthogonalValueConstraint> GetRelatedConstraints(Solver solver) =>
((IEnumerable<OrthogonalValueConstraint>)solver.Constraints<DifferenceConstraint>()).Concat(solver.Constraints<RatioConstraint>());

protected override int DefaultMarkerValue => 0;
}
}
2 changes: 1 addition & 1 deletion SudokuSolver/Constraints/DifferenceConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DifferenceConstraint(Solver sudokuSolver, string options) : base(sudokuSo
protected override bool IsPairAllowedAcrossMarker(int markerValue, int v0, int v1) => (v0 + markerValue == v1 || v1 + markerValue == v0);

protected override IEnumerable<OrthogonalValueConstraint> GetRelatedConstraints(Solver solver) =>
solver.Constraints<RatioConstraint>();
((IEnumerable<OrthogonalValueConstraint>)solver.Constraints<RatioConstraint>()).Concat(solver.Constraints<AnykropkiConstraint>());

protected override int DefaultMarkerValue => 1;
}
Expand Down
6 changes: 3 additions & 3 deletions SudokuSolver/Constraints/OrthogonalValueConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public abstract class OrthogonalValueConstraint : Constraint


private static readonly Regex negRegex = new(@"neg(\d*)");
private static readonly Regex twoCellsRegex = new(@"(\d*)r(\d+)c(\d+)r(\d+)c(\d+)");
private static readonly Regex sharedRowRegex = new(@"(\d*)r(\d+)[,-](\d+)c(\d+)");
private static readonly Regex sharedColRegex = new(@"(\d*)r(\d+)c(\d+)[,-](\d+)");
private static readonly Regex twoCellsRegex = new(@"(-?\d*)r(\d+)c(\d+)r(\d+)c(\d+)");
private static readonly Regex sharedRowRegex = new(@"(-?\d*)r(\d+)[,-](\d+)c(\d+)");
private static readonly Regex sharedColRegex = new(@"(-?\d*)r(\d+)c(\d+)[,-](\d+)");

public OrthogonalValueConstraint(Solver sudokuSolver, string options) : base(sudokuSolver)
{
Expand Down
2 changes: 1 addition & 1 deletion SudokuSolver/Constraints/RatioConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public RatioConstraint(Solver sudokuSolver, string options) : base(sudokuSolver,
protected override bool IsPairAllowedAcrossMarker(int markerValue, int v0, int v1) => (v0 * markerValue == v1 || v1 * markerValue == v0);

protected override IEnumerable<OrthogonalValueConstraint> GetRelatedConstraints(Solver solver) =>
solver.Constraints<DifferenceConstraint>();
((IEnumerable<OrthogonalValueConstraint>)solver.Constraints<DifferenceConstraint>()).Concat(solver.Constraints<AnykropkiConstraint>());

protected override int DefaultMarkerValue => 2;
}
Expand Down
1 change: 1 addition & 0 deletions SudokuSolver/PuzzleFormats/FPuzzlesBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public record FPuzzlesBoard(
FPuzzlesCells[] difference,
FPuzzlesCells[] xv,
FPuzzlesCells[] ratio,
FPuzzlesCells[] anykropki,
FPuzzlesClone[] clone,
FPuzzlesQuadruple[] quadruple,
FPuzzlesLines[] betweenline,
Expand Down
33 changes: 33 additions & 0 deletions SudokuSolver/SolverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,29 @@ public static Solver CreateFromFPuzzles(string fpuzzlesURL, IEnumerable<string>
}
}

if (fpuzzlesData.anykropki != null && fpuzzlesData.anykropki.Length > 0)
{
StringBuilder anykropkiParams = new();

foreach (var anykropki in fpuzzlesData.anykropki)
{
if (anykropkiParams.Length > 0)
{
anykropkiParams.Append(';');
}
anykropkiParams.Append(anykropki.value);
foreach (var cell in anykropki.cells)
{
anykropkiParams.Append(cell);
}
}

if (anykropkiParams.Length > 0)
{
solver.AddConstraint(typeof(AnykropkiConstraint), anykropkiParams.ToString());
}
}

bool negativeXV = fpuzzlesData.negative?.Contains("xv") ?? false;
if (fpuzzlesData.xv != null && fpuzzlesData.xv.Length > 0 || negativeXV)
{
Expand Down Expand Up @@ -1062,6 +1085,15 @@ public static string ToFPuzzlesURL(Solver solver, bool justBase64 = false)
ratio.Add(new(new string[] { CN(cell1), CN(cell2) }, value));
}

List<FPuzzlesCells> anykropki = new();
foreach (var marker in solver.Constraints<AnykropkiConstraint>().SelectMany(c => c.markers))
{
var cell1 = (marker.Key.Item1, marker.Key.Item2);
var cell2 = (marker.Key.Item3, marker.Key.Item4);
string value = marker.Value != 2 ? marker.Value.ToString() : null;
anykropki.Add(new(new string[] { CN(cell1), CN(cell2) }, value));
}

List<FPuzzlesClone> clone = new();
foreach (var c in solver.Constraints<CloneConstraint>())
{
Expand Down Expand Up @@ -1135,6 +1167,7 @@ public static string ToFPuzzlesURL(Solver solver, bool justBase64 = false)
difference: difference.ToArray(),
xv: xv.ToArray(),
ratio: ratio.ToArray(),
anykropki: anykropki.ToArray(),
clone: clone.ToArray(),
quadruple: quadruple.ToArray(),
betweenline: betweenline.ToArray(),
Expand Down
Loading