diff --git a/day3/c/aoc_day3.c b/day3/c/aoc_day3.c new file mode 100644 index 0000000..f4069bb --- /dev/null +++ b/day3/c/aoc_day3.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#define ARR_LEN (1000) + + +// read file, dump into diagnostics and count bits, do part one stuff +// return answer for part one +int d3_part_one(int* diagnostics, size_t diag_len, + int* bit_ct, size_t bit_ct_len); + +// returns answer for part two +int d3_part_two(const int* const diagnostics, size_t diag_len, + const int* const bit_ct, size_t bit_len, + int power_consumption); + +int main(int argc, char** argv) +{ + int diagnostic_arr[ARR_LEN]; + int bit_ct[12]; + + memset(diagnostic_arr, 0, ARR_LEN * sizeof(int)); + memset(bit_ct, 0, 12 * sizeof(int)); + + + int power_consumption = d3_part_one(diagnostic_arr, ARR_LEN, bit_ct, 12); + printf("(Part 1 Answer) Power Consumption: %d\n", power_consumption); + + + return 0; +} + + +// returns status of init +int d3_part_one(int* diagnostics, size_t diag_len, + int* bit_ct, size_t bit_ct_len) +{ + FILE* fp = fopen("input.txt", "r"); + if(!fp) + { + printf("Failed to open file for reading...\n"); + exit(-1); + } + char* line = NULL; + size_t len = 0; + for(int ind = 0; ind < ARR_LEN; ind++) + { + ssize_t rc = getline(&line, &len, fp); + if(rc == -1) + { + printf("EXITING ABNORMALLY\n"); + exit(-1); + } + diagnostics[ind] = strtol(line, NULL, 2); + for(int bit = 0; bit < bit_ct_len; bit++) + { + bit_ct[bit] += ((diagnostics[ind] & (1 << bit))>>bit); + } + } + + fclose(fp); + + int gamma = 0; + int epsilon = 0; + for(int bit = 0; bit < bit_ct_len; bit++) + { + gamma += ((bit_ct[bit] > (ARR_LEN >> 1))<