Example #1
0
        private async Task PlayAsync(int frameCap = int.MaxValue)
        {
            IsPlaying = true;

            var cancellationToken = VisualizerHandler.GetVisualizationCancellationToken();
            List <List <(Day10.Point Pos, long Tile)> > frames = ((Day13)SolutionInstance).VisualizationFrames;

            char[][] tiles = null;
            foreach (var(frame, index) in frames.WithIndex())
            {
                if (index >= frameCap)
                {
                    break;
                }

                if (tiles == null)
                {
                    var width  = frame.Max(x => x.Pos.X) + 1;
                    var height = frame.Max(x => x.Pos.Y) + 1;
                    tiles = Enumerable.Range(0, height).Select(x => new char[width]).ToArray();
                }

                foreach (var(pos, tile) in frame)
                {
                    if (pos.X < 0)
                    {
                        Score = (int)tile;
                        continue;
                    }

                    char c;
                    switch ((Day13.Tile)tile)
                    {
                    case Day13.Tile.Empty: c = ' '; break;

                    case Day13.Tile.Wall: c = '▓'; break;

                    case Day13.Tile.Block: c = '■'; break;

                    case Day13.Tile.Paddle: c = '─'; break;

                    case Day13.Tile.Ball: c = 'o'; break;

                    default: c = '?'; break;
                    }
                    tiles[pos.Y][pos.X] = c;
                }

                Content = string.Join(Environment.NewLine, tiles.Select(line => new string(line)));
                StateHasChanged();

                try { await Task.Delay(10, cancellationToken); }
                catch (TaskCanceledException) { break; }
            }

            IsPlaying = false;
        }
Example #2
0
        private async Task PlayAsync(int frameCap = int.MaxValue)
        {
            IsPlaying = true;

            var cancellationToken = VisualizerHandler.GetVisualizationCancellationToken();
            var solution          = (Day15)SolutionInstance;
            var originalMap       = solution.Map;
            var topLeft           = new Point(originalMap.Keys.Min(p => p.X), originalMap.Keys.Min(p => p.Y));
            var bottomRight       = new Point(originalMap.Keys.Max(p => p.X), originalMap.Keys.Max(p => p.Y));
            var map = new Dictionary <Point, Tile>()
            {
                [topLeft]     = Tile.Unknown,
                [bottomRight] = Tile.Unknown,
                [Point.Empty] = Tile.Robot
            };

            var index = 0;

            foreach (var(pos, tile) in originalMap)
            {
                if (index++ >= frameCap)
                {
                    break;
                }
                if (index != 0)
                {
                    map[pos] = tile;
                }

                if (index % 5 == 0 && !await RenderMap(map, topLeft, bottomRight, cancellationToken))
                {
                    break;
                }
            }

            foreach (var(pos, pathIndex) in solution.PathToOxygenGenerator.WithIndex())
            {
                if (index++ >= frameCap || pathIndex + 1 >= solution.PathToOxygenGenerator.Count)
                {
                    break;
                }
                map[pos] = Tile.Empty;
                map[solution.PathToOxygenGenerator[pathIndex + 1]] = Tile.Robot;

                if (!await RenderMap(map, topLeft, bottomRight, cancellationToken, 5))
                {
                    break;
                }
            }

            foreach (var pos in solution.OxygenVisited)
            {
                if (index++ >= frameCap)
                {
                    break;
                }
                map[pos] = Tile.OxygenSystem;

                if (index % 2 == 0 && !await RenderMap(map, topLeft, bottomRight, cancellationToken))
                {
                    break;
                }
            }

            await RenderMap(map, topLeft, bottomRight, cancellationToken, 0);

            IsPlaying = false;
        }