diff options
Diffstat (limited to 'Advent-of-Code-2021/AOC-3')
| -rwxr-xr-x | Advent-of-Code-2021/AOC-3/build.sh | 7 | ||||
| -rw-r--r-- | Advent-of-Code-2021/AOC-3/example.txt (renamed from Advent-of-Code-2021/AOC-3/sample.txt) | 0 | ||||
| -rw-r--r-- | Advent-of-Code-2021/AOC-3/main.c | 127 | 
3 files changed, 76 insertions, 58 deletions
| diff --git a/Advent-of-Code-2021/AOC-3/build.sh b/Advent-of-Code-2021/AOC-3/build.sh new file mode 100755 index 0000000..6be9241 --- /dev/null +++ b/Advent-of-Code-2021/AOC-3/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -xe + +gcc -o main -Wall -Wextra main.c + +./main diff --git a/Advent-of-Code-2021/AOC-3/sample.txt b/Advent-of-Code-2021/AOC-3/example.txt index a6366a8..a6366a8 100644 --- a/Advent-of-Code-2021/AOC-3/sample.txt +++ b/Advent-of-Code-2021/AOC-3/example.txt diff --git a/Advent-of-Code-2021/AOC-3/main.c b/Advent-of-Code-2021/AOC-3/main.c index 51cab24..29d334e 100644 --- a/Advent-of-Code-2021/AOC-3/main.c +++ b/Advent-of-Code-2021/AOC-3/main.c @@ -1,86 +1,97 @@  #include <stdio.h>  #include <stdlib.h>  #include <string.h> -#include <assert.h> -#include <stdint.h> -#define N 5 -#define INP_COUNT 12 +#if 1 +    #define PART_1 +#else +    #define PART_2 +#endif -int table[N]; +#if 0 +    #define FILE_PATH "example.txt" +    #define NUMS 12 +    #define BITS 5 +#else +    #define FILE_PATH "input.txt" +    #define NUMS 1000 +    #define BITS 12 +#endif -void ParseInput(char *filepath) +int nums[NUMS] = {0}; + +void parse()  { -  char ch; -  FILE *fp; -  fp = fopen(filepath, "r"); +    FILE *fp = fopen(FILE_PATH, "r"); +    if(!fp) { +        fprintf(stderr, "ERROR: Could not open file %s\n", FILE_PATH); +        exit(EXIT_FAILURE); +    } -  if(fp == NULL) -  { -    fprintf(stderr, "ERROR: something with file idk what fuck you"); -    exit(EXIT_FAILURE); -  } +    char line[32]; +    int index = 0; +    while(fgets(line, sizeof(line), fp) != NULL) +    { +        for(int i = 0; i < BITS; i++) +            nums[index] |= (line[i] - '0') << (BITS-1 - i); + +        index++; +    } -  int i = 0; -  while((ch = fgetc(fp)) != EOF) -  { -    if(ch == '\n') { i = 0; continue; } +    fclose(fp); +} -    if(ch == '1') -      table[i] += 1; +ulong gen_gamma() +{ +    int most_common[BITS] = {0}; + +    for(int i = 0; i < BITS; i++) +    { +        int ones = 0; -    i += 1; -  } +        for(int j = 0; j < NUMS; j++) +            if(nums[j] & (1 << i)) ones++; -  fclose(fp); +        if(ones > NUMS/2) most_common[i] = 1; +    } + +    ulong gamma = 0; + +    for(int i = 0; i < BITS; i++) +        gamma |= most_common[i] << i; + +    return gamma;  } -void PrintTable() +void print_nums()  { -  for(int i=0; i<N; i++) -  { -    printf("bitplace %d: %d\n", i+1, table[i]); -  } +    for(int i = 0; i < NUMS; i++) +        printf("%b\n", nums[i]);  } -int PartOne() +void part_1()  { -  int gamma_val = 0; -  for(int i=0; i<N; i++) -  { -    assert(table[i] != (INP_COUNT/2)); -       -    if(table[i] > (INP_COUNT/2)) -    { -      gamma_val = gamma_val | (1 << (N-1-i)); -    } -  } - -  int epsilon_val = gamma_val; -  for(int i=0; i<N; i++) -  { -    epsilon_val = epsilon_val ^ (1 << i); -  } +    ulong gamma = gen_gamma(); +    ulong epsilon = (~gamma & ~(~0 << BITS)); -  int result = gamma_val * epsilon_val;   -  return result;   +    printf("Power consumption is %lu", gamma*epsilon);  } -int PartTwo() +void part_2()  { -   -   -  int result = 0; -  return result; +    exit(EXIT_FAILURE);  }  int main(void)  { -  memset(table, 0, sizeof(int)*N); -  ParseInput("sample.txt"); +    parse(); + +#ifdef PART_1 +    part_1(); +#endif +#ifdef PART_2 +    part_2(); +#endif -  PrintTable(); -  printf("Part 1-RESULT: %d\n", PartOne()); -  -  return 0; +    return 0;  } | 
