Example #1
0
        public PowerResult HighestPowerCube(int gridSize)
        {
            var      highestTotalPower = int.MinValue;
            FuelCell highestCell       = null;

            for (int rowIndex = 0; rowIndex < _size - (gridSize - 1); rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < _size - (gridSize - 1); columnIndex++)
                {
                    var currentTotalPower = 0;
                    for (int rowOffset = 0; rowOffset < gridSize; rowOffset++)
                    {
                        for (int columnOffset = 0; columnOffset < gridSize; columnOffset++)
                        {
                            var cellPower = Cells[rowIndex + rowOffset, columnIndex + columnOffset].PowerLevel;
                            currentTotalPower += cellPower;
                        }
                    }
                    if (highestTotalPower < currentTotalPower)
                    {
                        highestTotalPower = currentTotalPower;
                        highestCell       = Cells[rowIndex, columnIndex];
                    }
                }
            }

            return(new PowerResult {
                Cell = highestCell, TotalPower = highestTotalPower
            });
        }
Example #2
0
        public static void Run()
        {
            long largestResult = long.MinValue;
            int  largestSize   = 0;
            var  point         = Point.Empty;

            history = new Dictionary <int, FuelCellAggregate[, ]>();

            int input = 8141;

            FuelCellGridSize = new Size(300, 300);

            if (fuelCells == null)
            {
                fuelCells = new FuelCell[FuelCellGridSize.Width, FuelCellGridSize.Height];
                var fuelCellAggregates = new FuelCellAggregate[FuelCellGridSize.Width, FuelCellGridSize.Height];

                var perFuelCell = new Action <PerFuelCellDelegate>(dlgt => {
                    for (int x = 1; x <= FuelCellGridSize.Width; x++)
                    {
                        for (int y = 1; y <= FuelCellGridSize.Height; y++)
                        {
                            if (fuelCells[x - 1, y - 1] == null)
                            {
                                fuelCells[x - 1, y - 1] = new FuelCell();
                            }
                            dlgt?.Invoke(fuelCells[x - 1, y - 1], x, y);
                        }
                    }
                });

                history.Add(1, new FuelCellAggregate[300, 300]);

                perFuelCell((cell, x, y) => {
                    cell.Position            = new Point(x, y);
                    cell.RackId              = x + 10;
                    var powerLevel           = CalculatePowerLevel(cell.RackId, y, input);
                    cell.PowerLevel          = powerLevel;
                    history[1][x - 1, y - 1] = new FuelCellAggregate {
                        PowerLevel = powerLevel
                    };
                });
            }

            for (int size = 2; size <= 300; size++)
            {
                var result = RunOnce(size);

                if (result.PowerLevel > largestResult)
                {
                    largestSize   = size;
                    largestResult = result.PowerLevel;
                    point         = result.Coordinate;
                }
            }

            Console.WriteLine($"{point.X},{point.Y},{largestSize}");
        }
Example #3
0
 public CalculationTests()
 {
     _grid = new Grid {
         SerialNumber = 8
     };
     _calculator = new PowerCalculator( );
     _cell       = new FuelCell {
         X = 3, Y = 5
     };
 }
Example #4
0
        public int PowerLevel(int SerialNumber, FuelCell cell)
        {
            var rackId = RackId(cell);
            var step1  = Step1(rackId, cell);
            var step2  = Step2(step1, SerialNumber, cell);
            var step3  = Step3(step2, rackId);
            var step4  = Step4(step3);
            var power  = Step5(step4);

            return(power);
        }
Example #5
0
        public void CalculatesPowerLevel(int x, int y, int gridSerial, int expectedPowerLevel)
        {
            // Arrange
            var coordinate = new Point(x, y);

            // Act
            var fuelCell = new FuelCell(coordinate, gridSerial);

            // Assert
            Assert.That(fuelCell.PowerLevel, Is.EqualTo(expectedPowerLevel));
        }
        public void P11_TestFuelCellPowerLevelCalculation()
        {
            var fuelCell   = new FuelCell(122, 79, 57);
            var powerlevel = fuelCell.GetPowerLevel();

            Assert.AreEqual(-5, powerlevel);

            fuelCell   = new FuelCell(217, 196, 39);
            powerlevel = fuelCell.GetPowerLevel();
            Assert.AreEqual(0, powerlevel);

            fuelCell   = new FuelCell(101, 153, 71);
            powerlevel = fuelCell.GetPowerLevel();
            Assert.AreEqual(4, powerlevel);
        }
Example #7
0
    public FuelCellGrid(int gridSize, int serialNumber)
    {
        _gridSize = gridSize;
        _cellGrid = new FuelCell[gridSize, gridSize];

        for (var y = 1; y <= _gridSize; y++)
        {
            var rowSum = 0;
            for (var x = 1; x <= _gridSize; x++)
            {
                var fuelCell = new FuelCell(x, y, serialNumber);
                rowSum += fuelCell.PowerLevel;
                var cellAbove = GetCell(x, y - 1);
                fuelCell.SummedPowerLevel = rowSum + (cellAbove?.SummedPowerLevel ?? 0);
                AddCellToGrid(fuelCell);
            }
        }
    }
Example #8
0
        public void Initialize(int size, PowerCalculator calculator)
        {
            _size = size;
            Cells = new FuelCell[size, size];
            for (int rowIndex = 0; rowIndex < size; rowIndex++)
            {
                var currentY = rowIndex + 1;
                for (int columnIndex = 0; columnIndex < size; columnIndex++)
                {
                    var currentX = columnIndex + 1;

                    var cell = new FuelCell {
                        X = currentX, Y = currentY
                    };
                    var power = calculator.PowerLevel(SerialNumber, cell);
                    cell.PowerLevel = power;

                    Cells[rowIndex, columnIndex] = cell;
                }
            }
        }
Example #9
0
        public Grid(int serial)
        {
            Cells = new FuelCell[gridsize, gridsize];


            serialnumber = serial;
            for (int y = 1; y <= 300; y++)
            {
                for (int x = 1; x <= 300; x++)
                {
                    Cells[x - 1, y - 1] = new FuelCell();

                    Cells[x - 1, y - 1].x      = x;
                    Cells[x - 1, y - 1].y      = y;
                    Cells[x - 1, y - 1].serial = serial;

                    //The power level in a given fuel cell can be found through the following process:
                    //Find the fuel cell's rack ID, which is its X coordinate plus 10.
                    Cells[x - 1, y - 1].rackId = Cells[x - 1, y - 1].x + 10;

                    //Begin with a power level of the rack ID times the Y coordinate.
                    Cells[x - 1, y - 1].powerlevel = Cells[x - 1, y - 1].rackId * Cells[x - 1, y - 1].y;

                    //Increase the power level by the value of the grid serial number(your puzzle input)
                    Cells[x - 1, y - 1].powerlevel = Cells[x - 1, y - 1].powerlevel + Cells[x - 1, y - 1].serial;

                    //Set the power level to itself multiplied by the rack ID.
                    Cells[x - 1, y - 1].powerlevel = Cells[x - 1, y - 1].powerlevel * Cells[x - 1, y - 1].rackId;

                    //Keep only the hundreds digit of the power level(so 12345 becomes 3; numbers with no hundreds digit become 0).
                    int hundreds = (int)Math.Abs(Cells[x - 1, y - 1].powerlevel / 100 % 10);

                    //Subtract 5 from the power level.
                    Cells[x - 1, y - 1].powerlevel = hundreds - 5;
                }
            }

            SquaresList = new Dictionary <int, int[, ]>();
        }
Example #10
0
 private void AddCellToGrid(FuelCell fuelCell)
 {
     _cellGrid[fuelCell.XCoordinate - 1, fuelCell.YCoordinate - 1] = fuelCell;
 }
Example #11
0
        public static void Run()
        {
            int input     = 8141;
            int testInput = 8;

            //input = testInput;

            FuelCellGridSize = new Size(300, 300);

            var fuelCells          = new FuelCell[FuelCellGridSize.Width, FuelCellGridSize.Height];
            var fuelCellAggregates = new FuelCellAggregate[FuelCellGridSize.Width, FuelCellGridSize.Height];

            var perFuelCell = new Action <PerFuelCellDelegate>(dlgt => {
                for (int x = 1; x <= FuelCellGridSize.Width; x++)
                {
                    for (int y = 1; y <= FuelCellGridSize.Height; y++)
                    {
                        dlgt?.Invoke(fuelCells[x - 1, y - 1], x, y);
                    }
                }
            });

            var perFuelCellAggregate = new Action <PerFuelCellAggregateDelegate>(dlgt => {
                for (int x = 0; x < FuelCellGridSize.Width; x++)
                {
                    for (int y = 0; y < FuelCellGridSize.Height; y++)
                    {
                        dlgt?.Invoke(fuelCellAggregates[x, y], x, y);
                    }
                }
            });

            perFuelCell((cell, x, y) => {
                fuelCells[x - 1, y - 1] = new FuelCell();
            });

            perFuelCell((cell, x, y) => {
                cell.Position   = new Point(x, y);
                cell.RackId     = x + 10;
                var powerLevel  = CalculatePowerLevel(cell.RackId, y, input);
                cell.PowerLevel = powerLevel;
            });

            perFuelCellAggregate((cell, x, y) => {
                fuelCellAggregates[x, y] = new FuelCellAggregate();
            });

            for (int x = 0; x < FuelCellGridSize.Width - 2; x++)
            {
                for (int y = 0; y < FuelCellGridSize.Height - 2; y++)
                {
                    for (int sx = 0; sx < 3; sx++)
                    {
                        for (int sy = 0; sy < 3; sy++)
                        {
                            fuelCellAggregates[x + 1, y + 1].PowerLevel = fuelCellAggregates[x + 1, y + 1].PowerLevel + fuelCells[x + sx, y + sy].PowerLevel;
                        }
                    }
                }
            }

            long largestPowerCellCenter  = long.MinValue;
            var  largestPowerCellTopLeft = Point.Empty;

            perFuelCellAggregate((cell, x, y) => {
                if (cell.PowerLevel > largestPowerCellCenter)
                {
                    largestPowerCellCenter  = cell.PowerLevel;
                    largestPowerCellTopLeft = new Point(x - 1, y - 1);
                }
            });

            Console.WriteLine($"Point X={largestPowerCellTopLeft.X+1}, Y={largestPowerCellTopLeft.Y+1}");
        }
Example #12
0
 // Step2 - Increase the power level by the value of the grid serial number (your puzzle input).
 public int Step2(int step1, int serialNumber, FuelCell cell)
 {
     return(step1 + serialNumber);
 }
Example #13
0
 // Step1 - Begin with a power level of the rack ID times the Y coordinate.
 public int Step1(int rackId, FuelCell cell)
 {
     return(rackId * cell.Y);
 }
Example #14
0
 // Find the fuel cell's rack ID, which is its X coordinate plus 10.
 public int RackId(FuelCell cell)
 {
     return(cell.X + 10);
 }
Example #15
0
        private void PartTwo()
        {
            FuelCell[,] AllFuelCells = new FuelCell[301, 301];
            int[,] sum = new int[301, 301];

            for (int i = 0; i < 301; i++)
            {
                for (int j = 0; j < 301; j++)
                {
                    sum[i, j] = 0;
                }
            }


            for (int y = 1; y <= 300; y++)
            {
                for (int x = 1; x <= 300; x++)
                {
                    //AllFuelCells[x, y] = new FuelCell(x, y, false);
                    int id = x + 10;
                    int p  = id * y + GRID_SERIAL_NUMBER;
                    p         = (p * id) / 100 % 10 - 5;
                    sum[y, x] = p + sum[y - 1, x]
                                + sum[y, x - 1]
                                - sum[y - 1, x - 1];
                }
            }

            int best = 0;
            int bx   = 0;
            int by   = 0;
            int bs   = 0;


            for (int size = 1; size <= 300; size++)
            {
                Console.WriteLine(size);
                for (int y = size; y <= 300; y++)
                {
                    //Console.WriteLine($"Checking X = {x}");

                    for (int x = size; x <= 300; x++)
                    {
                        //int[,] power = new int[3, 3];
                        int totalPower = sum[y, x]
                                         - sum[y - size, x]
                                         - sum[y, x - size]
                                         + sum[y - size, x - size];

                        if (totalPower > best)
                        {
                            bx   = x;
                            by   = y;
                            bs   = size;
                            best = totalPower;
                        }
                    }
                }
            }

            Console.WriteLine($"Largest Power Coordinate: {bx - bs + 1},{by - bs + 1},{bs}");
            Console.WriteLine($"Max fuel cell: {best}");
        }