Skip to content

Commit

Permalink
fix puzzle input
Browse files Browse the repository at this point in the history
  • Loading branch information
LE COURTOIS Sylvain committed Aug 18, 2023
1 parent 524e4cc commit 75e432f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
12 changes: 6 additions & 6 deletions AdventOfCode2022/Puzzles/BlizzardBasin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Initialize(string puzzleInput)
private List<(int x, int y, char c)>? BlizzardsDown;
public int Width;
public int Height;
private HashSet<(int x, int y)> Walls;
private HashSet<(int x, int y)>? Walls;
public Dictionary<(int x, int y, int t), (int x, int y, int t)>? Prev;
public List<(int x, int y)>? DeadEnds;
public bool ComputingCompleted;
Expand Down Expand Up @@ -113,7 +113,7 @@ private bool SearchForNextMove(Queue<(int x, int y)> search, Queue<(int x, int y
found = true;
break;
}
if ( !blizzardsPos.Contains(pos) && !Walls.Contains(pos) && !newSearch.Contains(pos))
if ( !blizzardsPos.Contains(pos) && !Walls!.Contains(pos) && !newSearch.Contains(pos))
{
Prev!.Add((pos.x, pos.y, Minute), (head.x, head.y, Minute - 1));
newSearch.Enqueue(pos);
Expand All @@ -133,10 +133,10 @@ private bool SearchForNextMove(Queue<(int x, int y)> search, Queue<(int x, int y
public HashSet<(int x, int y)> ComputeBlizzardsPos()
{
// compute blizzards positions
var blizzardsPos = BlizzardsRight.Select(e => ((e.x - 1 + Minute) % Width + 1, e.y)).ToHashSet();
blizzardsPos.UnionWith(BlizzardsLeft.Select(e => (Mod(e.x - 1 - Minute, Width) + 1, e.y)));
blizzardsPos.UnionWith(BlizzardsUp.Select(e => (e.x, Mod(e.y - 1 - Minute, Height) + 1)));
blizzardsPos.UnionWith(BlizzardsDown.Select(e => (e.x, (e.y - 1 + Minute) % Height + 1)));
var blizzardsPos = BlizzardsRight!.Select(e => ((e.x - 1 + Minute) % Width + 1, e.y)).ToHashSet();
blizzardsPos.UnionWith(BlizzardsLeft!.Select(e => (Mod(e.x - 1 - Minute, Width) + 1, e.y)));
blizzardsPos.UnionWith(BlizzardsUp!.Select(e => (e.x, Mod(e.y - 1 - Minute, Height) + 1)));
blizzardsPos.UnionWith(BlizzardsDown!.Select(e => (e.x, (e.y - 1 + Minute) % Height + 1)));
return blizzardsPos;
}

Expand Down
9 changes: 2 additions & 7 deletions AdventOfCode2022web/Pages/DefaultPuzzleView.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
@page "/DefaultPuzzleView/{PuzzleNumber:int}"
@inject PuzzleHelper puzzleHelper;
@inject HttpClient Http

<div class="container">
<div class="row">
<div class="col-md-6">
<PuzzleInput PuzzleSolver="_solver" PuzzleNumber="@PuzzleNumber" Changed="@UpdateView" Input="@_input" />
<PuzzleInput PuzzleSolver="_solver" PuzzleNumber="@PuzzleNumber" Changed="@UpdateView" />
</div>
<div class="col-md-6">
@if (puzzleHelper.Puzzles[PuzzleNumber].Type == typeof(BlizzardBasin))
Expand Down Expand Up @@ -40,15 +39,11 @@
StateHasChanged();
}

protected override async Task OnParametersSetAsync()
protected override void OnParametersSet()
{
var puzzle = puzzleHelper.Puzzles[PuzzleNumber];
_solver = (IIncrementalPuzzleSolver)(Activator.CreateInstance(puzzle.Type))!;
_input = (await Http.GetStringAsync($"sample-data/{puzzle.Type.Name}.txt")).Replace("\r", "");
base.OnParametersSet();
}

protected override async Task OnInitializedAsync()
{
}
}
11 changes: 0 additions & 11 deletions AdventOfCode2022web/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,3 @@
<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>Day 27 is a Sudoku Solver, with no relation with Advent of Code !</p>
@{
var puzzles = puzzleHelper.Puzzles.Values.OrderBy(x => x.Number).ToList();
for (var puzzleIndex = 0; puzzleIndex < puzzles.Count; puzzleIndex++)
{
var item = puzzles[puzzleIndex];
var pageName = $"{(typeof(IIncrementalPuzzleSolver).IsAssignableFrom(item.Type) ? "DefaultPuzzleView" : "puzzle")}/{item.Number}";
<NavLink class="nav-link" href="@(pageName)">
<b style="padding-right:5px">@item.Number</b><span>@item.Title</span>
</NavLink>
}
}
26 changes: 17 additions & 9 deletions AdventOfCode2022web/Shared/PuzzleInput.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@using AdventOfCode2022web.Puzzles;
@using System.Diagnostics;
@inject PuzzleHelper puzzleHelper;
@inject HttpClient Http


<h1>@(PuzzleInfo.Title)</h1>
<p>The code for this solution on <a href="https://github.com/sylvain69780/AdventOfCode2022web/blob/master/AdventOfCode2022/Puzzles/@(PuzzleInfo.Type.Name).cs">GitHub</a></p>
Expand All @@ -10,11 +12,9 @@
</div>
@if (_collapsePuzzleInput)
{
if (CurrentInput == string.Empty)
CurrentInput = Input!;
<div>
<label class="form-label">The data below is that provided for example. Paste your own user input from <a href="https://adventofcode.com/2022/day/@PuzzleNumber">Advent Of Code</a>.</label>
<pre><textarea class="form-control" aria-label="Puzzle Input" rows="10" @bind="CurrentInput" maxlength="10000000"></textarea></pre>
<pre><textarea class="form-control" aria-label="Puzzle Input" rows="10" @bind="_input" maxlength="10000000"></textarea></pre>
</div>
}

Expand Down Expand Up @@ -57,10 +57,9 @@
public int PuzzleNumber { get; set; }
[Parameter]
public Action<int>? Changed { get; set; }
[Parameter]
public string? Input { get; set; }

private string CurrentInput { get; set; } = string.Empty;
private string _input = string.Empty;
private int _puzzleNumber = -1;

private string Response { get; set; } = string.Empty;
private int Iterations { get; set; }
Expand All @@ -79,11 +78,9 @@

private async Task Solve(Func<IEnumerable<string>> part)
{
if (CurrentInput == string.Empty)
CurrentInput = Input!;
Response = string.Empty;
Iterations = 0;
PuzzleSolver!.Initialize(CurrentInput);
PuzzleSolver!.Initialize(_input);
_isTaskRunning = true;
_cancel = false;
_stopwatch.Start();
Expand All @@ -109,4 +106,15 @@
_stopwatch.Stop();
Changed!(_delayVisualizationValue);
}

protected override async Task OnParametersSetAsync()
{
if (PuzzleNumber != _puzzleNumber)
{
var puzzle = puzzleHelper.Puzzles[PuzzleNumber];
_input = (await Http.GetStringAsync($"sample-data/{puzzle.Type.Name}.txt")).Replace("\r", "");
_puzzleNumber = PuzzleNumber;
}
base.OnParametersSet();
}
}

0 comments on commit 75e432f

Please sign in to comment.