| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- namespace D11._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length == 0) return;
- int serial = int.Parse(args[0]);
- const int length = 300;
- var grid = new int[length, length];
- var summedGrid = new int[length, length];
- for (int x = 1; x <= length; ++x)
- {
- for (int y = 1; y <= length; ++y)
- {
- int id = (x + 10);
- int n = (id * y + serial) * id;
- int c = (n / 100) % 10;
- grid[x - 1, y - 1] = c - 5;
- }
- }
- int maxTotal = 0;
- (int x, int y, int g)? maxCoordinates = null;
- for (var size = 0; size < length; ++size)
- {
- for (int x = 0; x < length - size; ++x)
- for (int y = 0; y < length - size; ++y)
- {
- int total = SumSquare(grid, summedGrid, size, x, y);
- if (total > maxTotal)
- {
- maxTotal = total;
- maxCoordinates = (x + 1, y + 1, size + 1);
- }
- }
- }
- Console.WriteLine($"The answer is : ({maxCoordinates.Value.x},{maxCoordinates.Value.y},{maxCoordinates.Value.g})");
- }
- private static int SumSquare(int[,] grid, int[,] summedGrid, int size, int x, int y)
- {
- // summedGrid contains the sum previously calculated for size - 1
- int total = summedGrid[x, y];
- // new sum is obtained by taking the previous sum and adding the edges plus the corner
- for (int i = 0; i < size; ++i)
- total += grid[x + i, y + size] + grid[x + size, y + i];
- total += grid[x + size, y + size];
- summedGrid[x, y] = total;
- return total;
- }
- }
- }
|