Skip to content

Commit

Permalink
sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
LE COURTOIS Sylvain committed Aug 14, 2023
1 parent 5a0d965 commit 21c4fbb
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
5 changes: 4 additions & 1 deletion AdventOfCode2022/Puzzles/Sudoku.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Setup(string input)
PuzzleState = _puzzleInput;
}

private string Entropy(int position)
public string Entropy(int position)
{
var res = new HashSet<char>();
var (col, row) = (position % 9, position / 9);
Expand Down Expand Up @@ -41,7 +41,10 @@ public IEnumerable<string> SolveFirstPart()
var slotsWithMinimalEntropy = emptySlots.Select(x => (p: x, e: Entropy(x))).OrderBy(x => x.e.Length).ThenBy(x => x.p).ToArray();
var slot = slotsWithMinimalEntropy[0];
if (slot.e == string.Empty)
{
yield return FormatPuzzleState();
continue;
}
var sb = new StringBuilder(PuzzleState);
for (var i = 0; i<slot.e.Length;i++)
{
Expand Down
1 change: 0 additions & 1 deletion AdventOfCode2022web/Pages/DefaultPuzzleView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
public int PuzzleNumber { get; set; }
private string? _input { get; set; }
private IPuzzleSolverV3? _solver;
private int _animationDuration = 500;
public void UpdateView(int animationDuration)
{
StateHasChanged();
Expand Down
1 change: 1 addition & 0 deletions AdventOfCode2022web/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
<p>Yes it is possible to create a Web SPA application in C#, thanks to WebAssembly !</p>
<p>Here you will find my solutions to Advent of Code 2022 puzzles.</p>
<p>Some puzzles require a lot of computing power and it's not very compatible with the use of a web browser.</p>
<p>Day 27 is a Sudoku Solver, with no relation with Advent of Code !</p>
6 changes: 2 additions & 4 deletions AdventOfCode2022web/Pages/Puzzle.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</div>
<div class="mb-3">
<div class="mb-3">
<button class="btn btn-primary" disabled=@IsTask1Running @onclick="SolveFirstPart">Solve Part 1</button>
<button class="btn btn-primary" @onclick="SolveFirstPart">Solve Part 1</button>
</div>
<div><pre>@Output1</pre></div>
</div>
<div class="mb-3">
<div class="mb-3">
<button class="btn btn-primary" disabled=@IsTask2Running @onclick="SolveSecondPart">Solve Part 2</button>
<button class="btn btn-primary" @onclick="SolveSecondPart">Solve Part 2</button>
</div>
<div><pre>@Output2</pre></div>
</div>
Expand All @@ -30,8 +30,6 @@
private string? Input;
private string? Output1;
private string? Output2;
private bool IsTask1Running;
private bool IsTask2Running;
protected override async Task OnParametersSetAsync()
{
var puzzle = puzzleHelper.Puzzles[PuzzleNumber];
Expand Down
76 changes: 76 additions & 0 deletions AdventOfCode2022web/Pages/SudokuView.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
@page "/SudokuView/{PuzzleNumber:int}"
@inject PuzzleHelper puzzleHelper;
@inject HttpClient Http

<p>
This is a Sudoku solver, and it has really no relation with Advent Of Code !
</p>

<svg viewBox="-0.2 -0.2 9.2 9.8" class="img-fluid">
<defs>
<g id="sudoku-cell">
<rect x=".1" y=".1" width=".8" height=".8" fill="orange"></rect>
</g>
<g id="sudoku-bad-cell">
<rect x=".1" y=".1" width=".8" height=".8" fill="red"></rect>
</g>
<pattern id="gridPattern" width="1" height="1" patternUnits="userSpaceOnUse">
<rect width="1" height="1" fill="white" stroke="black" stroke-width="0.025" />
</pattern>
<pattern id="gridPattern2" width="3" height="3" patternUnits="userSpaceOnUse">
<rect width="3" height="3" fill="url(#gridPattern)" stroke="black" stroke-width="0.05" />
</pattern>
</defs>
<rect x="0" y="0" width="9.0" height="9.0" fill="url(#gridPattern2)"></rect>
@{
if ( _solver != null && _solver.PuzzleState.Length == 81)
{
var s = _solver.PuzzleState;
foreach (var y in Enumerable.Range(0, 9))
foreach (var x in Enumerable.Range(0, 9))
{
var c = s[x + y * 9];
if (c == '.')
{
c = ' ';
var r = _solver.Entropy(x + y * 9).Length;
if ( r == 0)
{
<use href="#sudoku-bad-cell" x="@x" y="@y" />
}
else
{
var e = (double)(10 - r) / 9;
<use href="#sudoku-cell" x="@x" y="@y" opacity="@(e.ToStringCSS())" />
<g transform="translate(.75,.76)">
<text x="@x" y="@y" font-family="Comic Sans" font-size=".3" dominant-baseline="middle" text-anchor="middle">@r</text>
</g>
}
}
<g>
<g transform="translate(.5,.56)">
<text x="@x" y="@y" font-family="Comic Sans" font-size=".8" dominant-baseline="middle" text-anchor="middle">@c</text>
</g>
</g>
}
}
}
</svg>

<PuzzleInput PuzzleSolver="_solver" PuzzleNumber="@PuzzleNumber" Changed="@UpdateView" Input="@_input" />

@code {
[Parameter]
public int PuzzleNumber { get; set; }
private string? _input { get; set; }
private Sudoku? _solver;
public void UpdateView(int animationDuration) => StateHasChanged();

protected override async Task OnInitializedAsync()
{
var puzzle = puzzleHelper.Puzzles[PuzzleNumber];
_solver = new Sudoku();
_input = (await Http.GetStringAsync($"sample-data/{puzzle.Type.Name}.txt")).Replace("\r", "");
_solver.Setup(_input);
}
}
2 changes: 2 additions & 0 deletions AdventOfCode2022web/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
pageName = $"BlizzardBasinView/{item.Number}";
if (item.Type == typeof(UnstableDiffusion))
pageName = "UnstableDiffusionView";
if (item.Type == typeof(Sudoku))
pageName = $"SudokuView/{item.Number}";
<div class="nav-item px-3" @onclick="ToggleNavMenu">
<NavLink class="nav-link" href="@(pageName)">
<span class="oi oi-puzzle-piece" aria-hidden="true"></span>
Expand Down

0 comments on commit 21c4fbb

Please sign in to comment.