From 1f3169bc83953a6d75765b723ee28c7a519df818 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 6 Dec 2021 22:10:58 +0800 Subject: [PATCH] Day 6 solutions --- .gitignore | 1 + 6/6-1.c | 90 +++++++++++++++++++++++++++++++ 6/6-2.c | 48 +++++++++++++++++ 6/6-2ridiculous_threads.c | 111 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 6/6-1.c create mode 100644 6/6-2.c create mode 100644 6/6-2ridiculous_threads.c diff --git a/.gitignore b/.gitignore index 1518e5d..1420039 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.out *.swp .vscode/ +.gdb_history diff --git a/6/6-1.c b/6/6-1.c new file mode 100644 index 0000000..8ca884d --- /dev/null +++ b/6/6-1.c @@ -0,0 +1,90 @@ +#include +#include +#include "../check_alloc.h" + +#define CYCLE 6 +#define STARTING_TIMER 8 +unsigned int INITIAL_CONDITION[] = {3, 4, 3, 1, 2}; + +// LIST DATASTRUCTURE +#define INITIAL_SIZE 20 + +typedef struct +{ + unsigned int *list; + size_t size; + size_t capacity; +} LIST; + +void free_list(LIST *list) +{ + free(list->list); + free(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(unsigned int)); + return p_list; +} + +void add_list(LIST *list, unsigned int elem) +{ + if (list->size >= list->capacity) + { + list->capacity *= 2; + list->list = realloc(list->list, list->capacity * sizeof(unsigned int)); + CHECK_ALLOC(list->list); + } + list->list[list->size] = elem; + list->size++; +} + +void print_list(LIST *list) +{ + for (size_t i = 0; i < list->size; i++) + { + printf("%d ", list->list[i]); + } + putchar('\n'); +} + +// + +void step_time(LIST *state) +{ + size_t size_before = state->size; + for (size_t i = 0; i < size_before; i++) + { + if (state->list[i] == 0) + { + state->list[i] = CYCLE; + add_list(state, STARTING_TIMER); + } + else + { + state->list[i]--; + } + } +} + +int main(int argc, char const *argv[]) +{ + LIST *state = new_list(); + size_t ic_size = sizeof(INITIAL_CONDITION) / sizeof(INITIAL_CONDITION[0]); + for (size_t i = 0; i < ic_size; i++) + { + add_list(state, INITIAL_CONDITION[i]); + } + for (size_t i = 0; i < 80; i++) + { + step_time(state); + printf("%lu\t", i); + fflush(stdout); + // print_list(state); + } + printf("\n%lu\n", state->size); + return 0; +} diff --git a/6/6-2.c b/6/6-2.c new file mode 100644 index 0000000..15e67ad --- /dev/null +++ b/6/6-2.c @@ -0,0 +1,48 @@ +#include +#include +#include + +// IC +#define TIMESPAN_DAYS 256 +unsigned long INITIAL_CONDITION[] = {3, 4, 3, 1, 2}; + +int main(int argc, char const *argv[]) +{ + // IC LOGIC + bool is_ic_done = false; + size_t ic_done_count = 0; + size_t ic_size = sizeof(INITIAL_CONDITION) / sizeof(INITIAL_CONDITION[0]); + + // Had to scratch this one out on paper to figure it out. + unsigned long cycles7[7] = {0}; + unsigned long cycles9[9] = {0}; + unsigned long total = 0; + + for (size_t t = 0; t < TIMESPAN_DAYS; t++) + { + // APPLY IC BY ADDING TO CYCLES AT OFFSETS SPECIFIED IN ICS + if (!is_ic_done) + { + for (size_t i = 0; i < ic_size; i++) + { + if (t == INITIAL_CONDITION[i]) + { + cycles9[t % 9]++; + total++; + ic_done_count++; + } + } + if (ic_done_count > ic_size) + is_ic_done = true; + } + + // UPDATE POPULATION + unsigned long v = cycles7[t % 7]; + total += cycles9[t % 9]; + total += v; + cycles7[t % 7] += cycles9[t % 9]; + cycles9[t % 9] += v; + } + printf("%lu\n", total); + return 0; +} diff --git a/6/6-2ridiculous_threads.c b/6/6-2ridiculous_threads.c new file mode 100644 index 0000000..13729a6 --- /dev/null +++ b/6/6-2ridiculous_threads.c @@ -0,0 +1,111 @@ +#include +#include +#include "../check_alloc.h" +#include + +// EXTENDING THE DUMB SOLUTION WITH THREADS - STILL BAD BECAUSE OF THE ALGORITHM'S BAD TIME COMPLEXITY. + +#define CYCLE 6 +#define STARTING_TIMER 8 +unsigned int INITIAL_CONDITION[] = {3, 4, 3, 1, 2}; + +// LIST DATASTRUCTURE +#define INITIAL_SIZE 20 + +typedef struct +{ + unsigned int *list; + size_t size; + size_t capacity; +} LIST; + +void free_list(LIST *list) +{ + free(list->list); + free(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(unsigned int)); + return p_list; +} + +void add_list(LIST *list, unsigned int elem) +{ + if (list->size >= list->capacity) + { + list->capacity *= 2; + list->list = realloc(list->list, list->capacity * sizeof(unsigned int)); + CHECK_ALLOC(list->list); + } + list->list[list->size] = elem; + list->size++; +} + +void print_list(LIST *list) +{ + for (size_t i = 0; i < list->size; i++) + { + printf("%d ", list->list[i]); + } + putchar('\n'); +} + +// + +void step_time(LIST *state) +{ + size_t size_before = state->size; + for (size_t i = 0; i < size_before; i++) + { + if (state->list[i] == 0) + { + state->list[i] = CYCLE; + add_list(state, STARTING_TIMER); + } + else + { + state->list[i]--; + } + } +} + +void *manage_part(void *args) +{ + LIST *state = (LIST *)args; + for (size_t i = 0; i < 256; i++) + { + step_time(state); + printf("%lu\t", i); + fflush(stdout); + // print_list(state); + } +} + +int main(int argc, char const *argv[]) +{ + size_t ic_size = sizeof(INITIAL_CONDITION) / sizeof(INITIAL_CONDITION[0]); + LIST *parts[ic_size]; + pthread_t threads[ic_size]; + + // MAKE THREADS AND STATE PARTS + for (size_t i = 0; i < ic_size; i++) + { + parts[i] = new_list(); + add_list(parts[i], INITIAL_CONDITION[i]); + pthread_create(&threads[i], NULL, manage_part, parts[i]); + } + + size_t total = 0; + for (size_t i = 0; i < ic_size; i++) + { + pthread_join(threads[i], NULL); + total += parts[i]->size; + } + + printf("\n%lu\n", total); + return 0; +}