Guest User

Untitled

a guest
Dec 17th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <malloc.h>
  6.  
  7. typedef int32_t s32;
  8.  
  9. struct vec2
  10. {
  11. s32 x;
  12. s32 y;
  13. };
  14.  
  15. vec2 dirTable[] = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } };
  16. vec2 neighborTable[] = { { 0, 1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { -1, -1 }, { -1, 0 }, { -1, 1 } };
  17.  
  18. s32 SumNeighbors(s32 *grid, vec2 pos);
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. // Guess
  23. s32 *grid = (s32 *)calloc(1024 * 1024, sizeof(s32));
  24.  
  25. s32 largest = 0;
  26. s32 goal = 277678;
  27.  
  28. vec2 pos = { 511, 511 };
  29. s32 dir = 0;
  30.  
  31. s32 stepIter = 0;
  32. s32 stepMax = 1;
  33. s32 spiralStep = 0; // 0,1 0,1 ...
  34.  
  35. // Fill first square
  36. grid[(1024 * pos.y) + pos.x] = 1;
  37.  
  38. for (s32 i = 1; i < goal; i++)
  39. {
  40. pos.x += dirTable[dir].x;
  41. pos.y += dirTable[dir].y;
  42.  
  43. s32 acc = grid[(1024 * pos.y) + pos.x];
  44.  
  45. stepIter++;
  46. if (stepIter == stepMax)
  47. {
  48. stepIter = 0;
  49.  
  50. dir++;
  51. if (dir > 3)
  52. {
  53. dir = 0;
  54. }
  55.  
  56. spiralStep++;
  57. if (spiralStep == 2)
  58. {
  59. spiralStep = 0;
  60.  
  61. stepMax++;
  62. }
  63. }
  64. }
  65.  
  66. free(grid);
  67.  
  68. printf("Largest: %d\n", largest);
  69.  
  70. system("pause");
  71.  
  72. return 0;
  73. }
  74.  
  75. s32 SumNeighbors(s32 *grid, vec2 pos)
  76. {
  77. s32 result = 0;
  78.  
  79. vec2 n = {};
  80. for (s32 ni = 0; ni < 8; ni++)
  81. {
  82. n = neighborTable[ni];
  83. result += grid[(1024 * (pos.y + n.y)) + (pos.x + n.y)];
  84. }
  85.  
  86. return result;
  87. }
Add Comment
Please, Sign In to add comment