Moortiii

advent_of_code_2017_day_1_2

Dec 18th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5.     size_t size;
  6.     size_t count;
  7.     int *input_array;
  8. } input_t;
  9.  
  10. input_t * initialize_input_struct();
  11. int traverse_list(input_t *input, int current_pos);
  12. void add_character(input_t *input, int input_data);
  13. void read_input(input_t *input);
  14. void parse_input(input_t *input);
  15.  
  16. int main() {
  17.     input_t *input = initialize_input_struct();
  18.     read_input(input);
  19.     parse_input(input);
  20.     return 0;
  21. }
  22.  
  23. void parse_input(input_t *input) {
  24.     // Keep track of the sum
  25.     int sum = 0;
  26.     // Keep track of the highest index
  27.     int highest_index = input->count -1;
  28.     // The increment is equal to the size of the list divided by two
  29.     int increment = input->count / 2;
  30.     // Keep track of the next index
  31.     int next_index;
  32.  
  33.     // Loop through all the items
  34.     for (int i = 0; i < input->count; ++i) {
  35.         // Keep track of the current index
  36.         int current_value = input->input_array[i];
  37.         // If we can safely increment the index and proceed, do so
  38.         if(i + increment <= highest_index) {
  39.             next_index = i + increment;
  40.         } else {
  41.             // Check the number of steps you can safely take
  42.             int valid_steps = highest_index - increment;
  43.             // Proceed the number of steps and subtract them from the total distance to go
  44.             int remaining_steps = increment - valid_steps;
  45.             // Start from 0 and move forward the remaining number of steps
  46.             next_index = remaining_steps - 1; // Subtract one to start from index 0
  47.         }
  48.  
  49.         int next_value = input->input_array[next_index];
  50.         // Check if the value in the current index is equal to the value in the next index
  51.         if(current_value == next_value) {
  52.             sum += current_value;
  53.             printf("%i is equal to %i, sum: %i\n", current_value, next_value, sum);
  54.         }
  55.     }
  56. }
  57.  
  58. void read_input(input_t *input) {
  59.     FILE* f = fopen("input2.txt", "r");
  60.     if(f) {
  61.         while(!feof(f))  {
  62.             char buffer;
  63.             fscanf(f, "%c", &buffer);
  64.             add_character(input, buffer - '0');
  65.         }
  66.     } else {
  67.         printf("Failed to open file stream\n");
  68.     }
  69.     input->count -= 1; // When the list is complete, there is one unused space, remove this one
  70. }
  71.  
  72. // Create an initial list with 10 elements
  73. input_t * initialize_input_struct() {
  74.     input_t *input = malloc(sizeof(input_t));
  75.     input->size = 10;
  76.     input->count = 0;
  77.     input->input_array = malloc(sizeof(int) * input->size);
  78.     return input;
  79. }
  80.  
  81. // Add characters to the list 1 by 1
  82. void add_character(input_t *input, int input_data) {
  83.     if(input->size == input->count) {
  84.         input->size += 10;
  85.         input->input_array = realloc(input->input_array, sizeof(int) * input->size);
  86.     }
  87.     input->input_array[input->count] = input_data;
  88.     input->count++;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment