diff --git a/3/3-1.c b/3/3-1.c new file mode 100644 index 0000000..fea17e2 --- /dev/null +++ b/3/3-1.c @@ -0,0 +1,45 @@ +#include +#include + +#define WIDTH 5 + +int main(int argc, char const *argv[]) +{ + FILE *p_file = fopen("input", "r"); + + uint n_one[WIDTH] = {0}; + uint length = 0; + + char line[WIDTH + 2]; + while (fgets(line, sizeof line, p_file) != NULL) + { + length++; + for (size_t i = 0; i < WIDTH; i++) + { + if (line[i] - '0') // 1 + { + n_one[i]++; + } + } + } + + // GET GAMMA/EPSILON + uint gamma = 0, epsilon = 0; + uint place = 1; + for (size_t i = WIDTH; i--;) + { + if (n_one[i] > length / 2) + { + gamma += place; + } + else + { + epsilon += place; + } + place *= 2; + } + + printf("%d\n", gamma * epsilon); + + return 0; +} diff --git a/3/3-2.c b/3/3-2.c new file mode 100644 index 0000000..64abf82 --- /dev/null +++ b/3/3-2.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include "../check_alloc.h" + +#define INPUT_PATH "input" +#define WIDTH 12 + +// LIST DATASTRUCTURE +#define INITIAL_SIZE 20 + +typedef struct +{ + char **list; + size_t size; + size_t capacity; +} LIST; + +void free_list(LIST *list) +{ + free(list->list); + free(list); +} +void delete_list(LIST *list) +{ + for (size_t i = 0; i < list->size; i++) + { + free(list->list[i]); + } + free_list(list); +} + +LIST *new_list(void) +{ + LIST *p_list = calloc(1, sizeof(LIST)); + p_list->capacity = INITIAL_SIZE; + p_list->list = calloc(p_list->capacity, sizeof(char *)); + return p_list; +} + +void add_list(LIST *list, char *elem) +{ + if (list->size >= list->capacity) + { + list->capacity *= 2; + list->list = realloc(list->list, list->capacity * sizeof(char *)); + CHECK_ALLOC(list->list); + } + list->list[list->size] = elem; + list->size++; +} + +// + +LIST *read_file(char *path) +{ + FILE *p_file = fopen(path, "r"); + LIST *p_list = new_list(); + char line[WIDTH + 2]; + while (fgets(line, sizeof line, p_file) != NULL) + { + add_list(p_list, strdup(line)); + } + return p_list; +} + +char common_value(LIST *list, char index) +{ + uint one = 0; + uint zero = 0; + for (size_t i = 0; i < list->size; i++) + { + if (list->list[i][index] - '0') + { + one++; + } + else + { + zero++; + } + } + return one >= zero; +} + +void partition(LIST *list, char index, bool co2, bool o2) +{ + if (index >= WIDTH || list->size == 1) + { + if (co2) + printf("CO2\n"); + else + printf("O2\n"); + printf("%lu\n", strtol(list->list[0], NULL, 2)); + return; + } + char common = common_value(list, index); + LIST *p_list_co2 = new_list(); + LIST *p_list_o2 = new_list(); + for (size_t i = 0; i < list->size; i++) + { + if ((list->list[i][index] - '0') == common) + { + if (o2) + add_list(p_list_o2, list->list[i]); + } + else + { + if (co2) + add_list(p_list_co2, list->list[i]); + } + } + if (o2) + partition(p_list_o2, index + 1, false, true); + if (co2) + partition(p_list_co2, index + 1, true, false); + free_list(p_list_o2); + free_list(p_list_co2); +} + +int main(int argc, char const *argv[]) +{ + LIST *list = read_file(INPUT_PATH); + partition(list, 0, true, true); + delete_list(list); + return 0; +} diff --git a/check_alloc.h b/check_alloc.h new file mode 100644 index 0000000..9c2384e --- /dev/null +++ b/check_alloc.h @@ -0,0 +1,9 @@ +#include + +#ifndef CHECK_ALLOC +#define CHECK_ALLOC(v) \ + if ((v) == NULL) \ + { \ + exit(EXIT_FAILURE); \ + } +#endif \ No newline at end of file