diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2022-12-07 12:56:20 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2022-12-07 12:56:20 +0100 |
commit | d2969bd72436e81a1c2d4c0ecb5d66cb6654fc89 (patch) | |
tree | 78b5d38b7a7b7318df4a21736cb7e43f91fe180a /05.c | |
parent | 2490e48c629cc02af93f5fb685803a00725e3037 (diff) |
day 5 part 2
Diffstat (limited to '05.c')
-rw-r--r-- | 05.c | 48 |
1 files changed, 29 insertions, 19 deletions
@@ -1,6 +1,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <stdbool.h> #include "queue.h" #include "input.h" @@ -8,16 +9,25 @@ #define EXPECTED1 70369L #define EXPECTED2 203002L #define STACK_COUNT 9 + +SLIST_HEAD(crate_list, crate) head[STACK_COUNT]; +struct crate { + char crate_char; + SLIST_ENTRY(crate) entries; +}; +struct crate* last_crate[STACK_COUNT]; -void part1(struct input_str* input) { - // parse stacks - SLIST_HEAD(crate_list, crate) head[STACK_COUNT]; - struct crate { - char crate_char; - SLIST_ENTRY(crate) entries; - }; - struct crate* last_crate[STACK_COUNT]; +void move_9001(int count, struct crate_list* from_head, struct crate* from_crate, struct crate_list* to_head) { + // use a recursion to move the crates from bottom up + if(count > 1) { + move_9001(count - 1, from_head, SLIST_NEXT(from_crate, entries), to_head); + } + SLIST_REMOVE(from_head, from_crate, crate, entries); + SLIST_INSERT_HEAD(to_head, from_crate, entries); +} +void move_crates(struct input_str* input, bool is_crane_9000) { + // parse stacks for(int i = 0; i < STACK_COUNT; i++) { SLIST_INIT(head + i); last_crate[i] = SLIST_FIRST(head + i); @@ -63,14 +73,17 @@ void part1(struct input_str* input) { //printf("move %d from %d to %d\n", crate_count, from, to); from--; to--; - for(int m = 0; m < crate_count; m++) { - struct crate* cc = SLIST_FIRST(head + from); - SLIST_REMOVE_HEAD(head + from, entries); - SLIST_INSERT_HEAD(head + to, cc, entries); - } + if(is_crane_9000) + for(int m = 0; m < crate_count; m++) { + struct crate* cc = SLIST_FIRST(head + from); + SLIST_REMOVE_HEAD(head + from, entries); + SLIST_INSERT_HEAD(head + to, cc, entries); + } + else + move_9001(crate_count, head + from, SLIST_FIRST(head + from), head + to); } - // print result, expected is BSDMQFLSP with my input + // print result for(int s = 0; s < STACK_COUNT; s++) { struct crate* cc = SLIST_FIRST(head + s); printf("%c", cc->crate_char); @@ -80,17 +93,14 @@ void part1(struct input_str* input) { // TODO free all the crates } -void part2(struct input_str* input) { -} - int main() { // read input struct input_str input; input_str_read(&input, INPUT); // do stuff - part1(&input); - part2(&input); + move_crates(&input, true); // BSDMQFLSP expected + move_crates(&input, false); // PGSQBFLDP expected // cleanup & exit input_str_free(&input); |