Guest User

advent of code day 4 C

a guest
Dec 4th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <time.h>
  6.      
  7. clock_t start, end;
  8. double cpu_time_used;
  9. //static int cmp(const void *a, const void *b) { return *(char *)a - *(char *)b; }
  10. // for pt2
  11.  
  12. int main(void)
  13. {
  14.     start = clock();
  15.     // open in the password file and check for errors
  16.     FILE *fp;
  17.     fp = fopen("pass.txt", "r");
  18.     if(fp == NULL)
  19.     {
  20.         printf("Failed to open password file\n");
  21.         exit(1); // exit with error if file not found
  22.     }
  23.     char string[100]; // string to read line in
  24.     char *tokenPtr; // pointer to token
  25.     char **arr; // pointer to array of strings
  26.     int i = 0; // iterator
  27.     int tokens = 1; // number of tokens
  28.     int check = 0; // has it failed
  29.     int count = 0; // number of valid passwords
  30.     while(fgets(string,100,fp)) // read till eof
  31.     {
  32.         // read string in from the file
  33.         //printf("%s", string); // print it
  34.         for(i = 0; string[i] != '\0'; i++) // find the amount of tokens in the string
  35.         {
  36.             if(string[i] == ' ')
  37.             {
  38.                
  39.                 tokens++;
  40.             }
  41.         }
  42.         string[i-1] = '\0'; // remove the \n
  43.         //printf("done %d strings found\n", tokens);
  44.         tokenPtr = strtok(string, " "); // tokenize the string
  45.         arr = malloc(tokens * sizeof(char*)); // malloc array for number of strings
  46.         for(i = 0; i < tokens; i++) // malloc a array for each string
  47.         {                           // arr is an array of pointers to strings
  48.             // for pt2 qsort(tokenPtr, strlen(tokenPtr), 1, (cmp));
  49.             arr[i] = malloc(20 * sizeof(char));
  50.             strcpy(arr[i], tokenPtr); // copy string into the newly allocated array
  51.             tokenPtr = strtok(NULL, " ");
  52.         }
  53.    
  54.         check = 0; // reset the check
  55.         for(int j = 0; j < tokens; j++)
  56.         {
  57.             for(i = 0; i < tokens; i++)
  58.             {
  59.                 if(strcmp(arr[j], arr[i]) == 0 && i != j)
  60.                 {
  61.                     //printf("strings are the same\n");
  62.                     check = 1; // check failed
  63.                 }
  64.             }
  65.         }
  66.         if(check != 1) // if no strings were the same count passed increment it
  67.         {
  68.             count++;
  69.         }
  70.         for(i = 0; i < tokens; i++) // free the strings
  71.         {
  72.             free(arr[i]);
  73.         }
  74.         tokens = 1; // reset the ammount of strings
  75.    
  76.     }
  77.     printf("valid passwords: %d\n", count);
  78.     end = clock();
  79.     double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
  80.     printf("program took %f seconds to execute\n", time_taken);
  81.     return 0;
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment