diff options
author | Orangerot <purple@orangerot.dev> | 2022-12-17 01:16:18 +0100 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2022-12-17 01:16:18 +0100 |
commit | 6596b48113ced669b206d2e1f1e8ba6edcba88a2 (patch) | |
tree | 02c7d01783390862d989df6c91aff968e03dd415 /2022/day05 |
adv2022 first 8 days
Diffstat (limited to '2022/day05')
-rw-r--r-- | 2022/day05/input1.txt | 9 | ||||
-rw-r--r-- | 2022/day05/input2.txt | 512 | ||||
-rwxr-xr-x | 2022/day05/main | bin | 0 -> 21080 bytes | |||
-rw-r--r-- | 2022/day05/main.c | 122 | ||||
-rw-r--r-- | 2022/day05/main2.c | 129 |
5 files changed, 772 insertions, 0 deletions
diff --git a/2022/day05/input1.txt b/2022/day05/input1.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/day05/input1.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 diff --git a/2022/day05/input2.txt b/2022/day05/input2.txt new file mode 100644 index 0000000..c1f5fb0 --- /dev/null +++ b/2022/day05/input2.txt @@ -0,0 +1,512 @@ +[T] [D] [L] +[R] [S] [G] [P] [H] +[G] [H] [W] [R] [L] [P] +[W] [G] [F] [H] [S] [M] [L] +[Q] [V] [B] [J] [H] [N] [R] [N] +[M] [R] [R] [P] [M] [T] [H] [Q] [C] +[F] [F] [Z] [H] [S] [Z] [T] [D] [S] +[P] [H] [P] [Q] [P] [M] [P] [F] [D] + 1 2 3 4 5 6 7 8 9 + +move 3 from 8 to 9 +move 2 from 2 to 8 +move 5 from 4 to 2 +move 7 from 1 to 4 +move 3 from 8 to 2 +move 3 from 2 to 7 +move 1 from 7 to 4 +move 3 from 2 to 9 +move 4 from 7 to 9 +move 1 from 5 to 2 +move 2 from 3 to 4 +move 5 from 9 to 5 +move 6 from 9 to 3 +move 5 from 9 to 5 +move 1 from 9 to 7 +move 2 from 3 to 1 +move 7 from 3 to 9 +move 2 from 7 to 2 +move 5 from 2 to 4 +move 1 from 2 to 9 +move 2 from 1 to 9 +move 7 from 6 to 1 +move 2 from 7 to 3 +move 2 from 3 to 9 +move 1 from 7 to 4 +move 1 from 9 to 2 +move 3 from 1 to 8 +move 2 from 3 to 4 +move 5 from 9 to 2 +move 1 from 3 to 9 +move 8 from 5 to 7 +move 1 from 6 to 1 +move 15 from 4 to 1 +move 4 from 2 to 5 +move 5 from 9 to 7 +move 1 from 9 to 5 +move 5 from 1 to 2 +move 3 from 8 to 9 +move 1 from 7 to 6 +move 11 from 1 to 2 +move 7 from 5 to 3 +move 4 from 2 to 6 +move 7 from 3 to 4 +move 3 from 5 to 9 +move 2 from 2 to 5 +move 5 from 1 to 8 +move 2 from 6 to 8 +move 3 from 8 to 9 +move 9 from 4 to 9 +move 9 from 7 to 4 +move 2 from 8 to 1 +move 1 from 8 to 7 +move 6 from 2 to 7 +move 5 from 2 to 4 +move 5 from 7 to 2 +move 2 from 1 to 7 +move 2 from 6 to 4 +move 7 from 7 to 1 +move 3 from 2 to 6 +move 1 from 8 to 7 +move 2 from 9 to 3 +move 2 from 3 to 1 +move 1 from 2 to 5 +move 4 from 6 to 5 +move 2 from 2 to 3 +move 3 from 5 to 7 +move 1 from 5 to 3 +move 9 from 1 to 7 +move 2 from 9 to 5 +move 13 from 4 to 1 +move 5 from 7 to 2 +move 3 from 3 to 1 +move 2 from 2 to 9 +move 1 from 2 to 7 +move 5 from 5 to 6 +move 2 from 2 to 4 +move 5 from 1 to 3 +move 9 from 7 to 8 +move 2 from 9 to 5 +move 3 from 5 to 4 +move 5 from 9 to 2 +move 10 from 4 to 8 +move 1 from 4 to 1 +move 2 from 8 to 4 +move 4 from 8 to 2 +move 3 from 6 to 8 +move 7 from 8 to 7 +move 10 from 9 to 3 +move 7 from 3 to 2 +move 11 from 2 to 3 +move 13 from 3 to 9 +move 1 from 6 to 3 +move 1 from 1 to 2 +move 1 from 2 to 8 +move 3 from 3 to 4 +move 1 from 2 to 9 +move 1 from 4 to 1 +move 10 from 8 to 3 +move 11 from 9 to 7 +move 1 from 6 to 2 +move 14 from 7 to 1 +move 2 from 2 to 9 +move 4 from 7 to 6 +move 1 from 2 to 4 +move 3 from 4 to 2 +move 4 from 2 to 9 +move 10 from 3 to 4 +move 3 from 6 to 1 +move 5 from 9 to 5 +move 5 from 5 to 8 +move 1 from 9 to 7 +move 2 from 9 to 6 +move 1 from 9 to 8 +move 2 from 4 to 8 +move 1 from 4 to 5 +move 2 from 3 to 1 +move 2 from 3 to 7 +move 27 from 1 to 2 +move 2 from 7 to 1 +move 9 from 4 to 6 +move 9 from 6 to 5 +move 5 from 8 to 6 +move 26 from 2 to 3 +move 1 from 2 to 5 +move 1 from 2 to 7 +move 1 from 8 to 4 +move 1 from 7 to 8 +move 24 from 3 to 5 +move 1 from 8 to 5 +move 1 from 4 to 3 +move 1 from 7 to 1 +move 1 from 8 to 9 +move 7 from 1 to 7 +move 8 from 6 to 4 +move 4 from 7 to 6 +move 1 from 3 to 9 +move 2 from 9 to 1 +move 3 from 7 to 9 +move 8 from 4 to 6 +move 3 from 9 to 1 +move 1 from 3 to 6 +move 1 from 8 to 2 +move 10 from 5 to 4 +move 1 from 3 to 8 +move 13 from 5 to 3 +move 1 from 2 to 9 +move 1 from 8 to 9 +move 1 from 3 to 8 +move 1 from 9 to 2 +move 3 from 6 to 9 +move 7 from 4 to 9 +move 4 from 3 to 9 +move 2 from 6 to 8 +move 2 from 4 to 5 +move 10 from 9 to 3 +move 1 from 1 to 9 +move 1 from 4 to 8 +move 1 from 1 to 4 +move 1 from 4 to 5 +move 4 from 6 to 3 +move 1 from 9 to 5 +move 1 from 6 to 9 +move 2 from 6 to 5 +move 1 from 9 to 2 +move 1 from 6 to 7 +move 18 from 5 to 2 +move 22 from 3 to 7 +move 19 from 7 to 1 +move 3 from 8 to 5 +move 4 from 9 to 3 +move 2 from 7 to 2 +move 1 from 8 to 1 +move 19 from 1 to 3 +move 2 from 7 to 5 +move 13 from 3 to 9 +move 4 from 1 to 2 +move 3 from 5 to 1 +move 11 from 9 to 1 +move 11 from 2 to 8 +move 3 from 9 to 3 +move 3 from 5 to 2 +move 2 from 1 to 4 +move 5 from 2 to 7 +move 12 from 1 to 5 +move 2 from 4 to 5 +move 9 from 5 to 8 +move 1 from 5 to 3 +move 4 from 2 to 3 +move 2 from 7 to 5 +move 6 from 2 to 8 +move 17 from 8 to 9 +move 2 from 9 to 6 +move 2 from 7 to 1 +move 15 from 9 to 6 +move 2 from 2 to 4 +move 9 from 8 to 5 +move 2 from 1 to 3 +move 12 from 6 to 2 +move 2 from 3 to 9 +move 5 from 6 to 3 +move 4 from 5 to 3 +move 11 from 3 to 4 +move 2 from 9 to 4 +move 6 from 5 to 2 +move 13 from 4 to 3 +move 1 from 4 to 5 +move 1 from 4 to 8 +move 18 from 2 to 6 +move 2 from 5 to 3 +move 1 from 8 to 3 +move 1 from 2 to 5 +move 1 from 7 to 8 +move 28 from 3 to 6 +move 2 from 3 to 4 +move 3 from 5 to 9 +move 2 from 5 to 9 +move 3 from 9 to 3 +move 5 from 3 to 4 +move 1 from 9 to 3 +move 1 from 9 to 1 +move 1 from 3 to 4 +move 45 from 6 to 2 +move 1 from 8 to 3 +move 2 from 4 to 6 +move 5 from 4 to 2 +move 1 from 3 to 7 +move 3 from 2 to 9 +move 1 from 4 to 8 +move 3 from 6 to 1 +move 42 from 2 to 8 +move 2 from 9 to 2 +move 4 from 2 to 6 +move 2 from 2 to 7 +move 1 from 9 to 6 +move 2 from 8 to 9 +move 4 from 1 to 8 +move 1 from 6 to 4 +move 1 from 4 to 8 +move 1 from 2 to 5 +move 3 from 7 to 4 +move 39 from 8 to 3 +move 7 from 8 to 5 +move 8 from 5 to 7 +move 35 from 3 to 1 +move 4 from 3 to 7 +move 10 from 7 to 2 +move 2 from 9 to 6 +move 3 from 4 to 2 +move 1 from 7 to 5 +move 1 from 7 to 8 +move 1 from 5 to 4 +move 12 from 1 to 6 +move 1 from 8 to 1 +move 1 from 4 to 5 +move 14 from 6 to 8 +move 9 from 8 to 6 +move 5 from 6 to 1 +move 11 from 2 to 9 +move 1 from 9 to 8 +move 6 from 8 to 3 +move 6 from 9 to 2 +move 8 from 1 to 9 +move 3 from 3 to 6 +move 7 from 1 to 4 +move 1 from 5 to 9 +move 8 from 9 to 8 +move 7 from 6 to 8 +move 1 from 9 to 3 +move 3 from 6 to 4 +move 3 from 9 to 1 +move 4 from 3 to 2 +move 1 from 6 to 7 +move 1 from 4 to 2 +move 13 from 1 to 7 +move 6 from 4 to 8 +move 1 from 7 to 3 +move 1 from 4 to 6 +move 1 from 9 to 5 +move 1 from 3 to 5 +move 19 from 8 to 9 +move 1 from 6 to 5 +move 6 from 9 to 2 +move 2 from 5 to 8 +move 1 from 5 to 2 +move 4 from 1 to 4 +move 8 from 9 to 4 +move 3 from 9 to 8 +move 2 from 9 to 1 +move 6 from 7 to 5 +move 12 from 4 to 2 +move 6 from 8 to 3 +move 1 from 4 to 1 +move 1 from 3 to 1 +move 13 from 2 to 3 +move 4 from 5 to 3 +move 1 from 4 to 9 +move 1 from 8 to 9 +move 12 from 3 to 2 +move 1 from 9 to 1 +move 2 from 5 to 9 +move 3 from 9 to 5 +move 1 from 7 to 5 +move 3 from 7 to 3 +move 1 from 5 to 4 +move 1 from 5 to 8 +move 9 from 2 to 3 +move 2 from 2 to 3 +move 3 from 1 to 9 +move 1 from 8 to 9 +move 3 from 9 to 1 +move 9 from 2 to 6 +move 1 from 9 to 5 +move 6 from 2 to 3 +move 2 from 6 to 9 +move 3 from 6 to 3 +move 1 from 4 to 3 +move 2 from 9 to 6 +move 2 from 7 to 2 +move 2 from 2 to 8 +move 24 from 3 to 7 +move 2 from 5 to 6 +move 2 from 8 to 2 +move 7 from 2 to 8 +move 8 from 3 to 6 +move 2 from 1 to 3 +move 1 from 1 to 2 +move 1 from 5 to 2 +move 15 from 7 to 4 +move 9 from 7 to 9 +move 7 from 9 to 1 +move 5 from 8 to 1 +move 4 from 1 to 4 +move 19 from 4 to 3 +move 22 from 3 to 5 +move 1 from 7 to 5 +move 9 from 5 to 4 +move 6 from 1 to 3 +move 6 from 3 to 1 +move 4 from 5 to 4 +move 1 from 2 to 1 +move 1 from 2 to 6 +move 4 from 6 to 1 +move 1 from 3 to 6 +move 3 from 6 to 3 +move 2 from 9 to 8 +move 2 from 5 to 3 +move 2 from 5 to 1 +move 10 from 6 to 4 +move 4 from 4 to 9 +move 7 from 4 to 3 +move 2 from 8 to 7 +move 4 from 9 to 3 +move 5 from 5 to 7 +move 1 from 5 to 1 +move 1 from 6 to 3 +move 1 from 8 to 4 +move 1 from 8 to 3 +move 13 from 4 to 5 +move 1 from 1 to 8 +move 6 from 5 to 3 +move 1 from 7 to 6 +move 5 from 7 to 6 +move 9 from 1 to 8 +move 1 from 8 to 4 +move 1 from 7 to 1 +move 1 from 4 to 1 +move 5 from 3 to 7 +move 3 from 7 to 9 +move 1 from 5 to 4 +move 6 from 8 to 6 +move 1 from 9 to 3 +move 2 from 9 to 5 +move 7 from 5 to 9 +move 1 from 7 to 5 +move 2 from 5 to 3 +move 10 from 6 to 8 +move 2 from 6 to 1 +move 1 from 4 to 9 +move 1 from 7 to 5 +move 8 from 8 to 2 +move 1 from 1 to 7 +move 1 from 9 to 7 +move 1 from 5 to 1 +move 3 from 9 to 8 +move 7 from 8 to 7 +move 6 from 7 to 1 +move 1 from 8 to 7 +move 4 from 7 to 1 +move 16 from 3 to 7 +move 4 from 3 to 1 +move 5 from 7 to 8 +move 16 from 1 to 4 +move 9 from 1 to 7 +move 1 from 3 to 4 +move 15 from 4 to 8 +move 1 from 3 to 1 +move 2 from 1 to 6 +move 2 from 4 to 9 +move 17 from 8 to 2 +move 6 from 9 to 5 +move 8 from 7 to 8 +move 2 from 6 to 9 +move 4 from 5 to 7 +move 2 from 8 to 5 +move 1 from 5 to 9 +move 11 from 2 to 6 +move 4 from 6 to 1 +move 5 from 2 to 8 +move 2 from 9 to 2 +move 1 from 9 to 3 +move 3 from 1 to 8 +move 1 from 3 to 6 +move 7 from 6 to 9 +move 2 from 5 to 4 +move 6 from 7 to 4 +move 4 from 8 to 1 +move 1 from 5 to 2 +move 1 from 6 to 1 +move 7 from 9 to 8 +move 2 from 7 to 9 +move 9 from 2 to 9 +move 5 from 9 to 3 +move 3 from 2 to 8 +move 4 from 8 to 7 +move 9 from 7 to 2 +move 3 from 1 to 3 +move 14 from 8 to 1 +move 2 from 8 to 3 +move 1 from 9 to 4 +move 3 from 7 to 9 +move 8 from 3 to 9 +move 2 from 2 to 7 +move 12 from 1 to 8 +move 4 from 1 to 6 +move 2 from 6 to 7 +move 1 from 6 to 7 +move 9 from 4 to 7 +move 9 from 7 to 4 +move 1 from 1 to 6 +move 2 from 3 to 6 +move 2 from 6 to 8 +move 12 from 9 to 8 +move 2 from 6 to 9 +move 2 from 9 to 7 +move 1 from 8 to 5 +move 5 from 7 to 5 +move 1 from 9 to 1 +move 3 from 4 to 1 +move 5 from 4 to 8 +move 4 from 1 to 7 +move 1 from 4 to 2 +move 19 from 8 to 4 +move 2 from 7 to 5 +move 14 from 8 to 5 +move 2 from 7 to 8 +move 3 from 9 to 8 +move 19 from 4 to 2 +move 9 from 2 to 4 +move 2 from 7 to 8 +move 15 from 5 to 9 +move 15 from 9 to 8 +move 1 from 5 to 9 +move 11 from 8 to 7 +move 4 from 5 to 8 +move 1 from 5 to 9 +move 2 from 9 to 5 +move 2 from 2 to 6 +move 14 from 2 to 9 +move 12 from 8 to 9 +move 3 from 8 to 4 +move 7 from 9 to 2 +move 4 from 7 to 9 +move 1 from 6 to 9 +move 1 from 7 to 5 +move 1 from 6 to 2 +move 3 from 5 to 4 +move 19 from 9 to 4 +move 1 from 5 to 1 +move 1 from 9 to 8 +move 1 from 1 to 7 +move 1 from 8 to 9 +move 4 from 7 to 2 +move 3 from 7 to 6 +move 18 from 4 to 2 +move 17 from 2 to 3 +move 2 from 6 to 8 +move 17 from 3 to 6 +move 13 from 2 to 1 +move 2 from 8 to 3 +move 2 from 2 to 9 +move 6 from 1 to 9 +move 1 from 3 to 4 +move 1 from 3 to 9 +move 8 from 6 to 4 +move 20 from 4 to 8 +move 3 from 4 to 8 +move 15 from 8 to 2 +move 11 from 2 to 6 +move 2 from 1 to 7 +move 7 from 9 to 8 +move 6 from 9 to 3 +move 1 from 6 to 5 diff --git a/2022/day05/main b/2022/day05/main Binary files differnew file mode 100755 index 0000000..1d67170 --- /dev/null +++ b/2022/day05/main diff --git a/2022/day05/main.c b/2022/day05/main.c new file mode 100644 index 0000000..f6af2ea --- /dev/null +++ b/2022/day05/main.c @@ -0,0 +1,122 @@ + +#include <stdio.h> +#include <stdlib.h> + +struct StackItem_; + +typedef struct StackItem_ { + char value; + struct StackItem_ *next, *prev; +} StackItem; + +typedef struct Stack_ { + StackItem *top, *bottom; +} Stack; + +void push_top(Stack *stack, StackItem *item) { + if (stack->top) + { + StackItem* old = stack->top; + old->next = item; + item->prev = old; + } + if (!stack->top) stack->bottom = item; + stack->top = item; +} + +void push_back(Stack *stack, StackItem *item) { + if (stack->bottom) + { + StackItem* old = stack->bottom; + old->prev = item; + item->next = old; + } + if (!stack->bottom) stack->top = item; + stack->bottom = item; +} + +StackItem* pop_top(Stack *stack) { + StackItem* old = stack->top; + stack->top = old->prev; + if (old->prev != 0) + { + stack->top->next = 0; + } + old->next = old->prev = 0; + + return old; +} + +StackItem* pop_back(Stack *stack) { + StackItem* old = stack->bottom; + stack->bottom = old->next; + stack->bottom->prev = 0; + old->next = old->prev = 0; + + return old; +} + +void printCargo(Stack *cargo, size_t len) +{ + for (int i = 0; i < len; i++) + { + for ( StackItem *e = cargo[i].bottom; e != 0; e = e->next ) + { + printf("%c", e->value); + } + printf("\n"); + } +} + +int main() +{ + char *line = 0; + size_t len; + Stack *cargo; + size_t cargos; + ssize_t nread; + + int amount, from, to; + + while ((nread = getline(&line, &len, stdin)) != 0) + { + if ( line[1] == '1' ) continue; + if ( line[0] == '\n' ) break; + if ( !cargo ) { + cargos = (nread+1)/4; + cargo = (Stack*)malloc( sizeof(Stack) * cargos ); + } + + for (int i = 0; i < cargos; i++ ) { + char load = line[i*4+1]; + if ( load != ' ' ) { + StackItem *box = malloc(sizeof(StackItem)); + box->value = line[i*4+1]; + box->next = box->prev = 0; + push_back(&cargo[i], box); + } + } + } + + printCargo(cargo, cargos); + printf("---------------\n"); + + while (scanf("move %d from %d to %d\n", &amount, &from, &to) != EOF) + { + printf( "%d %d %d\n", amount, from, to ); + for (int i = 0; i < amount; i++) + { + StackItem *item = pop_top(&cargo[from-1]); + push_top(&cargo[to-1], item); + + printCargo(cargo, cargos); + printf("---------------\n"); + } + } + + for ( int i = 0; i < cargos; i++ ) + { + printf( "%c", cargo[i].top->value ); + } + printf("\n"); +} diff --git a/2022/day05/main2.c b/2022/day05/main2.c new file mode 100644 index 0000000..f7fe805 --- /dev/null +++ b/2022/day05/main2.c @@ -0,0 +1,129 @@ + +#include <stdio.h> +#include <stdlib.h> + +struct StackItem_; + +typedef struct StackItem_ { + char value; + struct StackItem_ *next, *prev; +} StackItem; + +typedef struct Stack_ { + StackItem *top, *bottom; +} Stack; + +void push_top(Stack *stack, StackItem *item) { + if (stack->top) + { + StackItem* old = stack->top; + old->next = item; + item->prev = old; + } + if (!stack->top) stack->bottom = item; + stack->top = item; +} + +void push_back(Stack *stack, StackItem *item) { + if (stack->bottom) + { + StackItem* old = stack->bottom; + old->prev = item; + item->next = old; + } + if (!stack->bottom) stack->top = item; + stack->bottom = item; +} + +StackItem* pop_top(Stack *stack) { + StackItem* old = stack->top; + stack->top = old->prev; + if (old->prev != 0) + { + stack->top->next = 0; + } + old->next = old->prev = 0; + + return old; +} + +StackItem* pop_back(Stack *stack) { + StackItem* old = stack->bottom; + stack->bottom = old->next; + stack->bottom->prev = 0; + old->next = old->prev = 0; + + return old; +} + +void printCargo(Stack *cargo, size_t len) +{ + for (int i = 0; i < len; i++) + { + for ( StackItem *e = cargo[i].bottom; e != 0; e = e->next ) + { + printf("%c", e->value); + } + printf("\n"); + } +} + +int main() +{ + char *line = 0; + size_t len; + Stack *cargo; + size_t cargos; + ssize_t nread; + + int amount, from, to; + + Stack *temp = malloc(sizeof(Stack)); + + while ((nread = getline(&line, &len, stdin)) != 0) + { + if ( line[1] == '1' ) continue; + if ( line[0] == '\n' ) break; + if ( !cargo ) { + cargos = (nread+1)/4; + cargo = (Stack*)malloc( sizeof(Stack) * cargos ); + } + + for (int i = 0; i < cargos; i++ ) { + char load = line[i*4+1]; + if ( load != ' ' ) { + StackItem *box = malloc(sizeof(StackItem)); + box->value = line[i*4+1]; + box->next = box->prev = 0; + push_back(&cargo[i], box); + } + } + } + + printCargo(cargo, cargos); + printf("---------------\n"); + + while (scanf("move %d from %d to %d\n", &amount, &from, &to) != EOF) + { + printf( "%d %d %d\n", amount, from, to ); + for (int i = 0; i < amount; i++) + { + StackItem *item = pop_top(&cargo[from-1]); + push_top(temp, item); + } + for (int i = 0; i < amount; i++) + { + StackItem *item = pop_top(temp); + push_top(&cargo[to-1], item); + + printCargo(cargo, cargos); + printf("---------------\n"); + } + } + + for ( int i = 0; i < cargos; i++ ) + { + printf( "%c", cargo[i].top->value ); + } + printf("\n"); +} |