Example #1
0
        public override async ValueTask <string> Solve_1()
        {
            var grid          = _img;
            var infChar       = '.';
            var infCharToggle = _algo[0] == '#';

            for (int i = 0; i < 2; i++)
            {
                grid = new FiniteGrid2D <char>(grid, 1, infChar);
                var nextgrid = new FiniteGrid2D <char>(grid);
                foreach (var(pos, _) in grid)
                {
                    var area = AreaToIndex(grid.GetPointWith8Neighbors(pos, infChar));
                    nextgrid[pos] = _algo[area];
                }
                grid = nextgrid;
                if (infCharToggle)
                {
                    infChar = infChar == '.' ? '#' : '.';
                }
            }
            //Console.WriteLine(grid.ToString());

            return(grid.Count(cell => cell.value == '#').ToString());
        }
Example #2
0
        public override async ValueTask <string> Solve_2()
        {
            var grid          = _img;
            var infChar       = '.';
            var infCharToggle = _algo[0] == '#';

            for (int i = 0; i < 50; i++)
            {
                var inflated = grid.Bounds.InflatedCopy(1, 1);
                grid = new FiniteGrid2D <char>(inflated, Step);

                if (infCharToggle)
                {
                    infChar = infChar == '.' ? '#' : '.';
                }
            }
            //Console.WriteLine(grid.ToString());

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

            char Step(Point p)
            {
                var area = grid.GetPointWith8Neighbors(p, infChar);

                return(_algo[AreaToIndex(area)]);
            }
        }
Example #3
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();
            var map = Grid2D.FromFile("../../../input.txt");

            var seenRatings      = new HashSet <long>();
            var currentDiversity = CalcBiodiversity(map);

            while (!seenRatings.Contains(currentDiversity))
            {
                _   = seenRatings.Add(currentDiversity);
                map = new FiniteGrid2D <char>(map.Bounds.Size, p => Step(map, p));
                currentDiversity = CalcBiodiversity(map);
                Console.WriteLine(map.ToString());
            }

            Console.WriteLine($"Part 1: Biodiversity rating: {currentDiversity}.");

            map = Grid2D.FromFile("../../../input.txt");
            _maps.Add(0, map);

            for (int i = 0; i < 200; i++)
            {
                StepAllMaps();
            }
            var bugCount = _maps.Sum(m => m.Value.Count(t => t.value == '#'));

            Console.WriteLine($"Part 2: Overall bugs: {bugCount}.");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Example #4
0
        public Day_20()
        {
            var input = File.ReadAllLines(InputFilePath).ToArray();

            _algo = input[0];
            _img  = Grid2D.FromArray(input, 2.., ..);
        }
Example #5
0
        private static void StepAllMaps()
        {
            var(min, max)  = _maps.Keys.MinMax().Value;
            _maps[min - 1] = new FiniteGrid2D <char>(5, 5, '.');
            _maps[max + 1] = new FiniteGrid2D <char>(5, 5, '.');

            var newmaps = _maps.Select(kvp => (idx: kvp.Key, map: Step2(kvp.Key))).ToList();

            _maps = newmaps.ToDictionary(x => x.idx, x => x.map);
        }
Example #6
0
        private static FiniteGrid2D <char> Step2(int mapIndex)
        {
            var newMap = new FiniteGrid2D <char>(_maps[mapIndex]);

            foreach (var(pos, value) in _maps[mapIndex])
            {
                newMap[pos] = StepChar(value, GetNeighborCountOf(mapIndex, pos));
            }
            newMap[2, 2] = '?';
            return(newMap);
        }
Example #7
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();
            _laby = Grid2D.FromFile("../../../input.txt");

            var labels = ReadLabels().ToList();

            Debug.Assert(labels.Count == 2 * labels.Select(x => x.name).Distinct().Count() - 2);

            _portals = new Dictionary <Point, Point>();
            var openLabels = new Dictionary <string, Point>();

            foreach (var labelGroup in labels.ToLookup(x => x.name))
            {
                if (labelGroup.Count() == 2)
                {
                    var pos = labelGroup.Select(x => x.pos).ToArray();
                    _portals.Add(pos[0], pos[1]);
                    _portals.Add(pos[1], pos[0]);
                }
                else
                {
                    openLabels.Add(labelGroup.Key, labelGroup.First().pos);
                }
            }

            var search = new DijkstraSearch <Point>(EqualityComparer <Point> .Default, Expander);
            var path   = search.FindFirst(openLabels["AA"], p => p == openLabels["ZZ"]);

            Console.WriteLine($"Part 1: Path has {path.Length} steps.");


            _innerregion = _laby.Bounds.InflatedCopy(-3, -3);
            var search2 = new DijkstraSearch <(Point, int level)>(EqualityComparer <(Point, int)> .Default, Expander2);
            var path2   = search2.FindFirst((openLabels["AA"], 0), p => p.Item1 == openLabels["ZZ"] && p.level == 0);

            Console.WriteLine($"Part 2: Path has {path2.Length} steps.");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Example #8
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());
        }
Example #9
0
 public Day_15()
 {
     _grid = Grid2D.FromFile(InputFilePath);
 }
Example #10
0
 public WrappingXGrid2D(FiniteGrid2D <TNode> source) : base(source)
 {
     Width = Bounds.Width;
 }
Example #11
0
 public Day_11()
 {
     grid = Grid2D.FromFile(InputFilePath);
 }
Example #12
0
 public Day_11()
 {
     _input = Grid2D.FromFile(InputFilePath);
 }
Example #13
0
        private static char Step(FiniteGrid2D <char> map, Point p)
        {
            var neighborCount = map.Get4NeighborsOf(p).Count(n => map[n] == '#');

            return(StepChar(map[p], neighborCount));
        }
Example #14
0
        private static long CalcBiodiversity(FiniteGrid2D <char> map)
        {
            var gridwidth = map.Bounds.Width;

            return(map.Sum(t => (long)(t.value == '#' ? Math.Pow(2, t.pos.X + t.pos.Y * gridwidth) : 0)));
        }