1
0

Program.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. namespace D11._2
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. if (args.Length == 0) return;
  9. int serial = int.Parse(args[0]);
  10. const int length = 300;
  11. var grid = new int[length, length];
  12. var summedGrid = new int[length, length];
  13. for (int x = 1; x <= length; ++x)
  14. {
  15. for (int y = 1; y <= length; ++y)
  16. {
  17. int id = (x + 10);
  18. int n = (id * y + serial) * id;
  19. int c = (n / 100) % 10;
  20. grid[x - 1, y - 1] = c - 5;
  21. }
  22. }
  23. int maxTotal = 0;
  24. (int x, int y, int g)? maxCoordinates = null;
  25. for (var size = 0; size < length; ++size)
  26. {
  27. for (int x = 0; x < length - size; ++x)
  28. for (int y = 0; y < length - size; ++y)
  29. {
  30. int total = SumSquare(grid, summedGrid, size, x, y);
  31. if (total > maxTotal)
  32. {
  33. maxTotal = total;
  34. maxCoordinates = (x + 1, y + 1, size + 1);
  35. }
  36. }
  37. }
  38. Console.WriteLine($"The answer is : ({maxCoordinates.Value.x},{maxCoordinates.Value.y},{maxCoordinates.Value.g})");
  39. }
  40. private static int SumSquare(int[,] grid, int[,] summedGrid, int size, int x, int y)
  41. {
  42. // summedGrid contains the sum previously calculated for size - 1
  43. int total = summedGrid[x, y];
  44. // new sum is obtained by taking the previous sum and adding the edges plus the corner
  45. for (int i = 0; i < size; ++i)
  46. total += grid[x + i, y + size] + grid[x + size, y + i];
  47. total += grid[x + size, y + size];
  48. summedGrid[x, y] = total;
  49. return total;
  50. }
  51. }
  52. }