示例#1
0
        public override async ValueTask <string> Solve_1()
        {
            var flashCout = 0;

            for (int i = 0; i < 100; i++)
            {
                foreach (var item in _input)
                {
                    _input[item.pos]++;
                }
                List <(System.Drawing.Point pos, char value)> toFlash = new();
                do
                {
                    toFlash = _input.Where(x => x.value > '9' && x.value != '+').ToList();
                    foreach (var item in toFlash)
                    {
                        _input[item.pos] = '+';
                        flashCout++;
                        var neigh = _input.Get8NeighborsOf(item.pos);
                        foreach (var n in neigh.Where(x => _input[x] != '+'))
                        {
                            _input[n]++;
                        }
                    }
                } while (toFlash.Any());

                foreach (var item in _input.Where(x => x.value == '+'))
                {
                    _input[item.pos] = '0';
                }
            }
            return(flashCout.ToString());
        }
示例#2
0
        public override async ValueTask <string> Solve_1()
        {
            var steps = 0;
            // Key = Source
            var moves = new Dictionary <Point, (Point to, char c)>();

            do
            {
                moves.Clear();
                steps++;
                var eastMoves = new HashSet <Point>();
                foreach (var cc in _grid.Where(it => it.value == '>'))
                {
                    var neighbor = _grid.GetTupleWraparound(cc.pos.MoveTo(Direction.Right));
                    if (neighbor.value == '.')
                    {
                        moves.Add(cc.pos, (neighbor.pos, '>'));
                        eastMoves.Add(neighbor.pos);
                    }
                }
                foreach (var cc in _grid.Where(it => it.value == 'v'))
                {
                    var neighbor = _grid.GetTupleWraparound(cc.pos.MoveTo(Direction.Down));
                    if ((neighbor.value == '.' && !eastMoves.Contains(neighbor.pos)) ||
                        (neighbor.value == '>' && moves.ContainsKey(neighbor.pos)))
                    {
                        moves.Add(cc.pos, (neighbor.pos, 'v'));
                    }
                }
                if (moves.Count > 0)
                {
                    foreach (var sourceField in moves.Keys)
                    {
                        _grid[sourceField] = '.';
                    }

                    foreach (var(to, c) in moves.Values)
                    {
                        _grid[to] = c;
                    }
                }
            } while (moves.Count > 0);
            return(steps.ToString());
        }
示例#3
0
        public override string Solve_1()
        {
            var hasChanged = false;

            do
            {
                hasChanged = false;
                var newgrid = new FiniteGrid2D <char>(grid);
                foreach (var(pos, value) in grid.Where(cell => cell.value != '.'))
                {
                    var newv = Life(pos);
                    if (newv != null)
                    {
                        newgrid[pos] = newv.Value;
                        hasChanged   = true;
                    }
                }
                grid = newgrid;
            } while (hasChanged);

            return(grid.Count(cell => cell.value == '#').ToString());
        }