Ejemplo n.º 1
0
        private static string SolvePart(IEnumerable <string> input, Func <IDefinedSizeGrid <char>, IDefinedSizeGrid <char> > stepFunc)
        {
            var initial = SolidGrid <char> .Extract(input);

            var states = new List <IDefinedSizeGrid <char> > {
                initial
            };

            while (true)
            {
                var prevState = states.Last();
                var newState  = stepFunc(prevState).Solidify();
                states.Add(newState);

                if (prevState.Overlay(newState, (c1, c2) => c1 == c2)
                    .Windowed(initial.Region())
                    .AllCells()
                    .All(b => b))
                {
                    break;
                }
                ;
            }

            return(states.Last().AllCells().Count(c => c == '#').ToString());
        }
Ejemplo n.º 2
0
        private static int TreesForSlope(string[] input, GridPoint slope)
        {
            var patternWillRepeatTimes = (input.Length - 1) / slope.Y;

            return(SolidGrid <char> .Extract(input)
                   .Map(c => c == '#')
                   .Wrapping()
                   .Warp(p => new GridPoint(p.X * slope.X, p.Y * slope.Y))                    // Squish the world to just trees aligned to our slope.
                   .Warp(p => new GridPoint(p.X + p.Y, p.Y))                                  // Turn diagonals into verticals
                   .Windowed(new GridRegion(GridPoint.Origin, 1, patternWillRepeatTimes + 1)) // Focus on the now-vertical path we walked down
                   .AllCells()
                   .Count(t => t));
        }
Ejemplo n.º 3
0
        public void TestWarp(int x, int y, char expected)
        {
            var solidGrid = new SolidGrid <char>(new[]
            {
                new[] { 'A', 'B', 'C' },
                new[] { 'D', 'E', 'F' },
                new[] { 'G', 'H', 'I' },
                new[] { 'J', 'K', 'L' }
            });

            var warped = solidGrid.Warp(gp => new GridPoint(gp.X / 100, gp.Y / 1000));

            warped.Get(new GridPoint(x, y)).Should().Be(expected);
        }
Ejemplo n.º 4
0
        public void ShouldRotate(int x, int y, char expected)
        {
            var solidGrid = new SolidGrid <char>(new[]
            {
                new[] { 'A', 'B', 'C' },
                new[] { 'D', 'E', 'F' },
                new[] { 'G', 'H', 'I' },
                new[] { 'J', 'K', 'L' }
            });

            var rotated = solidGrid.RotateClockwise();

            rotated.Get(new GridPoint(x, y)).Should().Be(expected);
        }
Ejemplo n.º 5
0
        public void ShouldGetRows()
        {
            var solidGrid = new SolidGrid <char>(new[]
            {
                new[] { 'A', 'B', 'C' },
                new[] { 'D', 'E', 'F' },
                new[] { 'G', 'H', 'I' },
                new[] { 'J', 'K', 'L' }
            });

            var rows = solidGrid.Rows().Realize();

            rows.Count.Should().Be(4);
            rows[1].Should().Equal('D', 'E', 'F');
        }
Ejemplo n.º 6
0
        public void ShouldGetColumns()
        {
            var solidGrid = new SolidGrid <char>(new[]
            {
                new[] { 'A', 'B', 'C' },
                new[] { 'D', 'E', 'F' },
                new[] { 'G', 'H', 'I' },
                new[] { 'J', 'K', 'L' }
            });

            var columns = solidGrid.Columns().Realize();

            columns.Count.Should().Be(3);
            columns[1].Should().Equal('B', 'E', 'H', 'K');
        }
Ejemplo n.º 7
0
            public string Solve(string[] input)
            {
                var baseGrid = SolidGrid <char> .Extract(input).Map(x => x == '#').Solidify();

                var initialState = BuildInitialState(baseGrid);

                var state = initialState;

                for (var i = 0; i < 6; i++)
                {
                    state = NextStep(state);
                }

                return(state.Count.ToString());
            }
Ejemplo n.º 8
0
        private static HashSet <Point4D> BuildInitialState(SolidGrid <bool> baseGrid)
        {
            var initialState = new HashSet <Point4D>();

            foreach (var point in baseGrid.Region().AllPoints())
            {
                if (baseGrid.Get(point))
                {
                    var point3d = new Point4D(point.X, point.Y, 0);
                    initialState.Add(point3d);
                }
            }

            return(initialState);
        }
Ejemplo n.º 9
0
        public void TestWindowing()
        {
            var solidGrid = new SolidGrid <char>(new[]
            {
                new[] { 'A', 'B', 'C' },
                new[] { 'D', 'E', 'F' },
                new[] { 'G', 'H', 'I' },
                new[] { 'J', 'K', 'L' }
            });

            var windowed = solidGrid.Windowed(new GridRegion(new GridPoint(1, 2), 2, 2));

            windowed.Get(new GridPoint(0, 0)).Should().Be('H');
            windowed.Get(new GridPoint(1, 1)).Should().Be('L');
            windowed.Get(new GridPoint(1, 0)).Should().Be('I');
        }
Ejemplo n.º 10
0
        private Dictionary <int, IDefinedSizeGrid <bool> > ParseGrids(string[] input)
        {
            var chunks        = input.SplitList(line => line.Length == 0);
            var grids         = new Dictionary <int, IDefinedSizeGrid <bool> >();
            var tileNameRegex = new Regex(@"Tile (\d+):");

            foreach (var chunk in chunks)
            {
                if (chunk.Count == 0)
                {
                    continue;
                }
                var tileId = int.Parse(tileNameRegex.Match(chunk[0]).Groups[1].Value);
                grids[tileId] = SolidGrid <char> .Extract(chunk.Skip(1)).Map(c => c is '#').Solidify();
            }

            return(grids);
        }