summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2022/aoc-5/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'Advent-of-Code-2022/aoc-5/main.c')
-rw-r--r--Advent-of-Code-2022/aoc-5/main.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-5/main.c b/Advent-of-Code-2022/aoc-5/main.c
new file mode 100644
index 0000000..409cf6b
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-5/main.c
@@ -0,0 +1,147 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if 0
+ #define transfer transfer_part1
+#else
+ #define transfer transfer_part2
+#endif
+
+#if 0
+ #define FILENAME "sample.txt"
+ #define COLUMNS 3
+#else
+ #define FILENAME "input.txt"
+ #define COLUMNS 9
+#endif
+
+#define STRINT(d) atoi(strtok(NULL, d)); strtok(NULL, d);
+
+typedef struct crate crate;
+crate *create_crate(char data);
+void push(crate *c, int i);
+crate *add(crate *c, crate *last, int i);
+crate *pop(int i);
+void transfer_part1(int from, int to, int n);
+void transfer_part2(int from, int to, int n);
+void free_crates(crate *c);
+void print_crate(char *format, crate *c);
+
+crate *top[COLUMNS+1] = {0};
+
+void parse()
+{
+ FILE *fp = fopen(FILENAME, "r");
+ if(!fp) {
+ fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME);
+ exit(1);
+ }
+
+ char line[256] = {0};
+ crate *last[COLUMNS] = {0};
+ while(fgets(line, sizeof(line), fp) != NULL && line[0] != '\n')
+ {
+ for(int i = 0; i < COLUMNS; i++) {
+ char ch = line[(i*4)+1];
+ if(ch >= 65 && ch <= 90) {
+ last[i] = add(create_crate(ch), last[i], i);
+ }
+ }
+ }
+
+ while(fgets(line, sizeof(line), fp) != NULL)
+ {
+ strtok(line, " ");
+ int n = STRINT(" ");
+ int from = STRINT(" ");
+ int to = STRINT(" ");
+
+ transfer(from-1, to-1, n);
+ }
+
+ fclose(fp);
+}
+
+int main(void)
+{
+ parse();
+ for(int i = 0; i < COLUMNS; i++) {
+ print_crate("%c", top[i]);
+ free_crates(top[i]);
+ }
+ puts("");
+ return 0;
+}
+
+
+
+struct crate {
+ char data;
+ struct crate *prev;
+};
+
+crate *create_crate(char data)
+{
+ crate *c = malloc(sizeof(crate));
+ c->data = data;
+ c->prev = NULL;
+ return c;
+}
+
+void push(crate *c, int i)
+{
+ c->prev = top[i];
+ top[i] = c;
+}
+
+crate *add(crate *c, crate *last, int i)
+{
+ if(last == NULL) {
+ push(c, i);
+ return c;
+ }
+
+ last->prev = c;
+ return c;
+}
+
+crate *pop(int i)
+{
+ if(top[i] == NULL) {
+ fprintf(stderr, "ERROR: Column %d is empty, can't pop\n", i);
+ exit(1);
+ }
+
+ crate *c = top[i];
+ top[i] = c->prev;
+ return c;
+}
+
+void transfer_part1(int from, int to, int n)
+{
+ for(int i = 0; i < n; i++)
+ push(pop(from), to);
+}
+
+void transfer_part2(int from, int to, int n)
+{
+ for(int i = 0; i < n; i++)
+ push(pop(from), COLUMNS);
+ for(int i = 0; i < n; i++)
+ push(pop(COLUMNS), to);
+}
+
+void free_crates(crate *c)
+{
+ if(c == NULL) return;
+ crate *prev = c->prev;
+ free(c);
+ free_crates(prev);
+}
+
+void print_crate(char *format, crate *c)
+{
+ if(c == NULL) return;
+ printf(format, c->data);
+}