summaryrefslogtreecommitdiff
path: root/05.c
diff options
context:
space:
mode:
Diffstat (limited to '05.c')
-rw-r--r--05.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/05.c b/05.c
index b127da7..4122c46 100644
--- a/05.c
+++ b/05.c
@@ -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);