working
parent
f60d45391f
commit
db622cd1f7
@ -0,0 +1,83 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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))<<bit);
|
||||||
|
}
|
||||||
|
epsilon = (~gamma) & 0xFFF;
|
||||||
|
return gamma * epsilon;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns answer for part two
|
||||||
|
// we can use the answer from part 1 to cut out a lot of unnecessary instructions
|
||||||
|
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 ans = 0;
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
Loading…
Reference in New Issue