Munashedov

Untitled

Dec 6th, 2021
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. // 2021-12-03
  2. // Advent of Code 2021 Day 3
  3. // Munashe
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <limits.h>
  10.  
  11. void printBinary(int decimal);
  12. int reverseBits(int input, int bitWidth);
  13. int invertBits(int input, int maskSize);
  14. int compareBitColumn(int *array, int len, int col);
  15. void sieve(int *result, int *input, int length, int col, int searchingMostCommon);
  16.  
  17. int main(void){
  18.     int CAP = 1000;
  19.     int width = 12;
  20.    
  21.     char buff[width];
  22.     int numbers[CAP];
  23.    
  24.     char fileName[] = "input.txt";
  25.     FILE *fp = fopen(fileName, "r");
  26.    
  27.     int count = 0;
  28.     if(!fp) { fprintf(stderr, "Could not open %s\n", fileName); }
  29.     while(fscanf(fp, "%s\n", buff) != EOF){
  30.         numbers[count] = strtol(buff, NULL, 2);
  31.         memset(&buff, 0, sizeof(buff[0])*width);
  32.         ++count;
  33.     }
  34.     fclose(fp);
  35.    
  36.     //------------------------------------------------------------------------------------------------------
  37.     // Part One
  38.     int gamma = 0;
  39.     for(int i = 0; i < width; ++i){ gamma = gamma << 1; gamma += compareBitColumn(numbers, count, i); }
  40.     gamma = reverseBits(gamma, width);
  41.     int epsilon = invertBits(gamma, width);
  42.    
  43.     printf("Power: %d\n", gamma * epsilon);
  44.    
  45.     //------------------------------------------------------------------------------------------------------
  46.     // Part Two
  47.  
  48.     for(int i = 0; i < count; ++i){ numbers[i] = reverseBits(numbers[i], width); }
  49.    
  50.     int oxygen = 0;
  51.     int scrubber = 0;
  52.     int result = 0;
  53.    
  54.     sieve(&result, numbers, count, 0, 1);
  55.     oxygen = reverseBits(result, width);
  56.    
  57.     sieve(&result, numbers, count, 0, 0);
  58.     scrubber = reverseBits(result, width);;
  59.    
  60.     printf("Life Support: %d\n", oxygen * scrubber);
  61.     return 0;
  62. }
  63.  
  64. void printBinary(int decimal){
  65.     printf("0b");
  66.     for(int i = 0; i < 32; ++i){
  67.         if(i % 8 == 0 && i != 0) { printf(" ");}
  68.         printf("%c", decimal % 2 ? '1' : '0');
  69.         decimal >>= 1;
  70.     }
  71.     printf("\n");
  72. }
  73.  
  74. int reverseBits(int input, int bitWidth){
  75.     int flipped = 0;
  76.     for(int i = 0; i < bitWidth; ++i){
  77.         flipped = flipped << 1; flipped += ((input >> i) & 1);
  78.     }
  79.     return flipped;
  80. }
  81.  
  82. int invertBits(int input, int maskSize){
  83.     return (~input) ^ (UINT_MAX << maskSize);
  84. }
  85.  
  86. int compareBitColumn(int *array, int len, int col){
  87.     int bitRatio = 0;
  88.     for(int j = 0; j < len; ++j){
  89.         if((array[j] >> col) & 1) { bitRatio++; } else { bitRatio--; }
  90.     }
  91.     return (bitRatio >= 0);
  92. }
  93.  
  94. void sieve(int *result, int *input, int length, int col, int searchingMostCommon){
  95.     if(length == 1){
  96.         *result = input[0];
  97.     } else {
  98.         int *newStack = malloc(sizeof(input[0])*length);
  99.         int newLength = 0;
  100.         int mostCommon = compareBitColumn(input, length, col);;
  101.         if(!searchingMostCommon) { mostCommon = (mostCommon == 1) ? 0 : 1; }
  102.         for(int j = 0; j < length; ++j){
  103.             if(mostCommon == 1){
  104.                 if((input[j] >> col) & 1) { newStack[newLength++] = input[j]; }
  105.             } else {
  106.                 if(((input[j] >> col) & 1) == 0) { newStack[newLength++] = input[j]; }
  107.             }
  108.         }
  109.         sieve(result, newStack, newLength, col+1, searchingMostCommon);
  110.     }
  111. }
Add Comment
Please, Sign In to add comment