day 3 finally finished... complicated solution for a simple question because im dumb

stable
Penguin 3 years ago
parent db622cd1f7
commit 5d88192d0d

@ -1,69 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARR_LEN (1000)
int main(int argc, char** argv)
{
FILE* fp = fopen("input.txt", "r");
if(!fp)
{
printf("FUCK");
return -1;
}
int bit_ct[12];
int diagnostic_arr[ARR_LEN];
memset(diagnostic_arr, 0, ARR_LEN * sizeof(int));
memset(bit_ct, 0, 12 * sizeof(int));
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)
{
break;
printf("FUCK\n");
return -1;
}
diagnostic_arr[ind] = strtol(line, NULL, 2);
for(int bit = 0; bit < 12; bit++)
{
bit_ct[bit] += ((diagnostic_arr[ind] & (1 << bit))>>bit);
}
}
/* uncomment this if u want to make ur code run like python */
/* int mat[1000][10000000]; */
/* for(int zind = 0; zind < 1000; zind++) */
/* { */
/* for(int zzind = 0; zzind < 10000000; zzind++) */
/* { */
/* mat[zzind][zind]= 0; */
/* } */
/* } */
int gamma = 0;
int dar = 0;
for(int bit = 0; bit < 12; bit++)
{
gamma += ((bit_ct[bit] > (ARR_LEN >> 1))<<bit);
dar += ((bit_ct[bit] < (ARR_LEN >> 1))<<bit);
printf("%d ", bit_ct[bit]);
}
printf("\n");
printf("solution %d\n", gamma*dar);
fclose(fp);
return 0;
}

@ -0,0 +1,256 @@
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
p bit_ct
p bit
p *ctr_wite
p *ctr_write
p *diag_write@*ctr_write
p/t *diag_write@*ctr_write
c
del
b 173
c
p/t *diag_write@*ctr_write
p/t *diag_write
p/t *ctr_write
q
b 181 if *ctr_write == 1
r
p *diag_write@*ctr_write
p/t *diag_write@*ctr_write
b 181 if *ctr_write == 4
r
p *diag_write@*ctr_write
p/t *diag_write@*ctr_write
n
n
n
n
n
p bit
p *diag_read@*ctr_read
n
n
n
n
n
p/t *diag_read@*ctr_read
q
b 166
r
b 181
p *diag_read@*ctr_read
p/t *diag_read@*ctr_read
c
c
c
b 181
b 181
c
r
c
b 182
del
b 166
b 182
c
c
c
b 184
cv
c
c
n
n
n
n
n
n
n
n
n
nn
n
n
n
n
n
nn
n
del
b 165
b 181
c
c
c
c
c
c
c
c
c
c
q
b 166
b 181
c
r
c
p *diag_write@*ctr_write
p/t *diag_write@*ctr_write
p bit
c
p *diag_read@*ctr_read
p/t *diag_read@*ctr_read
p *diag_read@*ctr_read
c
p *diag_write@*ctr_write
p/t *diag_write@*ctr_write
c
n
n
p bit_ct
p bit_ctn
n
load
q
b 157
b 166
b 181
r
c
c
c
p bit
c
c
c
p bit
n
p *diag_read@*ctr_read
p/t *diag_read@*ctr_read
p diag_read[ind] >> bit
p/t diag_read[ind] >> bit
p/t diag_read[ind] >> bit & 0x01
n
n
p bit_ct
n
p bit_ct
q
b 185
r
n
c
n
c
n
c
n
c
del
b 185 if *ctr_write == 1
r
n
p *diag_write
p/t *diag_write
p/t *diag_write@*ctr_write
p/t *diag_write@*ctr_write+1
p/t *diag_write@*ctr_write+2
n
p o2_gen_rating
n
p o2_gen_rating
p diag_write[*ctr_write]
del
b 157
b 166
b 181
r
c
c
p *diag_write@*ctr_write
c
c
p *ctr_read
c
p *diag_write@*ctr_write
r
c
c
c
c
p bit_ct
n
p bit_ct
n
p bit_ct
q
b 157
b 166
b 181
r
c
c
c
c
p bit_ct
r
c
c
c
p *ctr_read
p/t *diag_read@*ctr_read
n
p/t diag_read[ind]
p/t diag_read[ind] >> bit
p/t diag_read[ind] >> bit & 0x01
n
n
p bit_ct
p *ctr_read
c
p bit_ct
r
c
c
c
n
n
n
n
p diag_read[ind]
p/t diag_read[ind]
p/t diag_read[ind] >> bit
p bit
p/t diag_read[ind]
c
n
p *ctr_read
p bit_ct
n
n
p bit_ct
p *ctr_read
q
q

@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-std=gnu99 -O2 -g
PROJECT=aoc_day3
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
$(PROJECT): aoc_day3.o
$(CC) -o aoc_day3 aoc_day3.o

Binary file not shown.

@ -1,42 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define ARR_LEN (1000)
#define BIT_AMOUNT (12)
#define INPUT_FILE ("input.txt")
#define BIT_MASK (0xFFF)
// read file, dump into diagnostics and count bits
void d3_init(int* diagnostics, size_t diag_len,
int* bit_ct, size_t bit_ct_len);
// 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 d3_part_two(const int* const diagnostics, size_t diag_len);
// if binary we're assuming bit size is 12 because this project is specific to 12 bit numbers
static void print_arr(const int* const arr, size_t len, bool binary)
{
if(binary)
{
printf("\n");
for(int ind = 0; ind < len; ind++)
{
printf("[%d]: ", ind);
for(int bit = BIT_AMOUNT - 1; bit >= 0; bit--)
{
printf("%d", ((arr[ind] & (1 << bit))>>bit));
}
printf("\n");
}
}
else
{
for(int ind = 0; ind < len; ind++)
{
printf("[%d]: %d\n", ind, arr[ind]);
}
}
}
int main(int argc, char** argv)
{
int diagnostic_arr[ARR_LEN];
int bit_ct[12];
int bit_ct[BIT_AMOUNT];
memset(diagnostic_arr, 0, ARR_LEN * sizeof(int));
memset(bit_ct, 0, 12 * sizeof(int));
memset(bit_ct, 0, BIT_AMOUNT * sizeof(int));
d3_init(diagnostic_arr, ARR_LEN, bit_ct, BIT_AMOUNT);
int power_consumption = d3_part_one(diagnostic_arr, ARR_LEN, bit_ct, 12);
int power_consumption = d3_part_one(diagnostic_arr, ARR_LEN, bit_ct, BIT_AMOUNT);
printf("(Part 1 Answer) Power Consumption: %d\n", power_consumption);
int life_support_rating = d3_part_two(diagnostic_arr, ARR_LEN);
printf("(Part 2 Answer) Life support rating: %d\n", life_support_rating);
return 0;
}
// returns status of init
int d3_part_one(int* diagnostics, size_t diag_len,
// read file, dump into diagnostics and count bits
void d3_init(int* diagnostics, size_t diag_len,
int* bit_ct, size_t bit_ct_len)
{
FILE* fp = fopen("input.txt", "r");
FILE* fp = fopen(INPUT_FILE, "r");
if(!fp)
{
printf("Failed to open file for reading...\n");
@ -53,31 +90,156 @@ int d3_part_one(int* diagnostics, size_t diag_len,
exit(-1);
}
diagnostics[ind] = strtol(line, NULL, 2);
for(int bit = 0; bit < bit_ct_len; bit++)
for(int bit = 0; bit < BIT_AMOUNT; bit++)
{
printf("%d", (diagnostics[ind] & (1<<bit))>>bit);
bit_ct[bit] += ((diagnostics[ind] & (1 << bit))>>bit);
}
printf("\n");
}
fclose(fp);
}
// returns status of init
int d3_part_one(int* diagnostics, size_t diag_len,
int* bit_ct, size_t bit_ct_len)
{
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;
epsilon = (~gamma) & BIT_MASK;
printf("Epsilon: %d\t gamma: %d\n", epsilon, gamma);
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 d3_part_two(const int* const diagnostics, size_t diag_len)
{
int o2_gen_rating = 0;
int co2_scrubber_rating = 0;
int ans = 0;
// alternating active buffers
int diag_copy[2][ARR_LEN + 1];
// we copy diagnostics into our second diag_copy arr
// this way we start with diag_copy[0] being zeroed
// and diag_copy[1] having our original data.
// from there we will go back and forth, filtering data
// from eachother
memcpy(&diag_copy[(~(BIT_AMOUNT - 1)%2) & 0x01], diagnostics, diag_len * sizeof(int));
// last value will be active count, i.e. how many vars are actually in the array
diag_copy[(BIT_AMOUNT - 1)%2][ARR_LEN] = 0;
diag_copy[(~(BIT_AMOUNT - 1)%2) & 0x01][ARR_LEN] = ARR_LEN;
// finding oxygen generator rating
for(int bit = BIT_AMOUNT - 1; bit >= 0; bit--)
{
int* ctr_write = &diag_copy[bit%2][ARR_LEN];
int* ctr_read = &diag_copy[~(bit%2) & 0x01][ARR_LEN];
int* diag_write = diag_copy[bit%2];
const int* diag_read = diag_copy[~(bit%2) & 0x01];
memset(diag_write, 0, (sizeof(int) * ARR_LEN) + sizeof(int));
volatile int bit_ct = 0;
for(int ind = 0; ind < *ctr_read; ind++)
{
if((diag_read[ind] >> bit) & 0x01)
{
bit_ct++;
}
}
for(int ind = 0; ind < *ctr_read; ind++)
{
if(bit_ct >= (*ctr_read - bit_ct))
{
if((diag_read[ind] >> bit) & 0x01)
diag_write[(*ctr_write)++] = diag_read[ind];
}
else
{
if(!((diag_read[ind] >> bit) & 0x01))
{
diag_write[(*ctr_write)++] = diag_read[ind];
}
}
}
if(*ctr_write <= 1)
{
printf("found o2 rating\n");
o2_gen_rating = diag_write[*ctr_write-1];
break;
}
}
printf("o2 rating: %d\n", o2_gen_rating);
// now we need to do a similar thing for co2 scubber rating
// this time we want to find the least common bit instead of most common bit
// we copy diagnostics into our second diag_copy arr
// this way we start with diag_copy[0] being zeroed
// and diag_copy[1] having our original data.
// from there we will go back and forth, filtering data
// from eachother
memcpy(&diag_copy[(~(BIT_AMOUNT - 1)%2) & 0x01], diagnostics, diag_len * sizeof(int));
// last value will be active count, i.e. how many vars are actually in the array
diag_copy[(BIT_AMOUNT - 1)%2][ARR_LEN] = 0;
diag_copy[(~(BIT_AMOUNT - 1)%2) & 0x01][ARR_LEN] = ARR_LEN;
// finding oxygen generator rating
for(int bit = BIT_AMOUNT - 1; bit >= 0; bit--)
{
int* ctr_write = &diag_copy[bit%2][ARR_LEN];
int* ctr_read = &diag_copy[~(bit%2) & 0x01][ARR_LEN];
int* diag_write = diag_copy[bit%2];
const int* diag_read = diag_copy[~(bit%2) & 0x01];
memset(diag_write, 0, (sizeof(int) * ARR_LEN) + sizeof(int));
int bit_ct = 0;
for(int ind = 0; ind < *ctr_read; ind++)
{
if((diag_read[ind] >> bit) & 0x01)
{
bit_ct++;
}
}
for(int ind = 0; ind < *ctr_read; ind++)
{
if(bit_ct < (*ctr_read - bit_ct))
{
if((diag_read[ind] >> bit) & 0x01)
diag_write[(*ctr_write)++] = diag_read[ind];
}
else
{
if(!((diag_read[ind] >> bit) & 0x01))
{
diag_write[(*ctr_write)++] = diag_read[ind];
}
}
}
if(*ctr_write <= 1)
{
printf("found co2 \n");
co2_scrubber_rating = diag_write[*ctr_write-1];
break;
}
}
return ans;
printf("o2 gen rating: %d\nco2 scrubber rating: %d\n", o2_gen_rating, co2_scrubber_rating);
return co2_scrubber_rating * o2_gen_rating;
}

Binary file not shown.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
Loading…
Cancel
Save