Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #include <stdint.h>
- typedef int32_t s32;
- static s32 total;
- s32 ConsumeNode(s32 *data, s32 index);
- int main(int argc, char *argv[])
- {
- s32 data_count = 0;
- s32 *data = (s32 *)calloc(1024*1024, sizeof(s32));
- FILE *file = 0;
- fopen_s(&file, "input.txt", "rb");
- if (file)
- {
- // Note: Had to replace spaces with commas to get scanf to read properly
- s32 n = 0;
- while (fscanf(file, "%d, ", &n) == 1)
- {
- data[data_count++] = n;
- }
- fclose(file);
- }
- ConsumeNode(data, 0);
- printf("Total: %d\n", total);
- return 0;
- }
- // Returns new index
- s32 ConsumeNode(s32 *data, s32 index)
- {
- s32 i = index;
- s32 child_count = data[i++];
- s32 meta_data_count = data[i++];
- printf("%d %d %d\n", i, child_count, meta_data_count);
- // Iterate through child nodes
- for (s32 k = 0; k < child_count; k++)
- {
- i = ConsumeNode(data, i);
- }
- // Total meta data values
- s32 meta_data_total = 0;
- for (s32 j = 0; j < meta_data_count; j++)
- {
- meta_data_total += data[i++];
- }
- // Dump meta data total to global
- total += meta_data_total;
- return i;
- }
Add Comment
Please, Sign In to add comment