From 75e432f9ce2259575ac401519102eb4ece8ab63f Mon Sep 17 00:00:00 2001 From: LE COURTOIS Sylvain Date: Fri, 18 Aug 2023 17:00:28 +0200 Subject: [PATCH] fix puzzle input --- AdventOfCode2022/Puzzles/BlizzardBasin.cs | 12 ++++----- .../Pages/DefaultPuzzleView.razor | 9 ++----- AdventOfCode2022web/Pages/Index.razor | 11 -------- AdventOfCode2022web/Shared/PuzzleInput.razor | 26 ++++++++++++------- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/AdventOfCode2022/Puzzles/BlizzardBasin.cs b/AdventOfCode2022/Puzzles/BlizzardBasin.cs index b1f3bd39..63bb7e4b 100644 --- a/AdventOfCode2022/Puzzles/BlizzardBasin.cs +++ b/AdventOfCode2022/Puzzles/BlizzardBasin.cs @@ -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; @@ -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); @@ -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; } diff --git a/AdventOfCode2022web/Pages/DefaultPuzzleView.razor b/AdventOfCode2022web/Pages/DefaultPuzzleView.razor index 6e822d45..1deeb2d5 100644 --- a/AdventOfCode2022web/Pages/DefaultPuzzleView.razor +++ b/AdventOfCode2022web/Pages/DefaultPuzzleView.razor @@ -1,11 +1,10 @@ @page "/DefaultPuzzleView/{PuzzleNumber:int}" @inject PuzzleHelper puzzleHelper; -@inject HttpClient Http
- +
@if (puzzleHelper.Puzzles[PuzzleNumber].Type == typeof(BlizzardBasin)) @@ -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() - { - } } diff --git a/AdventOfCode2022web/Pages/Index.razor b/AdventOfCode2022web/Pages/Index.razor index 22be47e0..2827b509 100644 --- a/AdventOfCode2022web/Pages/Index.razor +++ b/AdventOfCode2022web/Pages/Index.razor @@ -11,14 +11,3 @@

Yes it is possible to create a Web SPA application in C#, thanks to WebAssembly !

Here you will find my solutions to Advent of Code 2022 puzzles.

Day 27 is a Sudoku Solver, with no relation with Advent of Code !

-@{ - 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}"; - - @item.Number@item.Title - - } -} diff --git a/AdventOfCode2022web/Shared/PuzzleInput.razor b/AdventOfCode2022web/Shared/PuzzleInput.razor index 1e33ac72..2f88304d 100644 --- a/AdventOfCode2022web/Shared/PuzzleInput.razor +++ b/AdventOfCode2022web/Shared/PuzzleInput.razor @@ -1,6 +1,8 @@ @using AdventOfCode2022web.Puzzles; @using System.Diagnostics; @inject PuzzleHelper puzzleHelper; +@inject HttpClient Http +

@(PuzzleInfo.Title)

The code for this solution on GitHub

@@ -10,11 +12,9 @@
@if (_collapsePuzzleInput) { - if (CurrentInput == string.Empty) - CurrentInput = Input!;
-
+
} @@ -57,10 +57,9 @@ public int PuzzleNumber { get; set; } [Parameter] public Action? 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; } @@ -79,11 +78,9 @@ private async Task Solve(Func> part) { - if (CurrentInput == string.Empty) - CurrentInput = Input!; Response = string.Empty; Iterations = 0; - PuzzleSolver!.Initialize(CurrentInput); + PuzzleSolver!.Initialize(_input); _isTaskRunning = true; _cancel = false; _stopwatch.Start(); @@ -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(); + } }