Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. typedef int32_t s32;
  7.  
  8. static s32 total;
  9.  
  10. s32 ConsumeNode(s32 *data, s32 index);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. s32 data_count = 0;
  15. s32 *data = (s32 *)calloc(1024*1024, sizeof(s32));
  16.  
  17. FILE *file = 0;
  18. fopen_s(&file, "input.txt", "rb");
  19. if (file)
  20. {
  21. // Note: Had to replace spaces with commas to get scanf to read properly
  22. s32 n = 0;
  23. while (fscanf(file, "%d, ", &n) == 1)
  24. {
  25. data[data_count++] = n;
  26. }
  27.  
  28. fclose(file);
  29. }
  30.  
  31. ConsumeNode(data, 0);
  32.  
  33. printf("Total: %d\n", total);
  34.  
  35. return 0;
  36. }
  37.  
  38. // Returns new index
  39. s32 ConsumeNode(s32 *data, s32 index)
  40. {
  41. s32 i = index;
  42. s32 child_count = data[i++];
  43. s32 meta_data_count = data[i++];
  44.  
  45. printf("%d %d %d\n", i, child_count, meta_data_count);
  46.  
  47. // Iterate through child nodes
  48. for (s32 k = 0; k < child_count; k++)
  49. {
  50. i = ConsumeNode(data, i);
  51. }
  52.  
  53. // Total meta data values
  54. s32 meta_data_total = 0;
  55. for (s32 j = 0; j < meta_data_count; j++)
  56. {
  57. meta_data_total += data[i++];
  58. }
  59.  
  60. // Dump meta data total to global
  61. total += meta_data_total;
  62.  
  63. return i;
  64. }
Add Comment
Please, Sign In to add comment