summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-12-05 18:33:52 +0200
committerkartofen <mladenovnasko0@gmail.com>2022-12-05 18:33:52 +0200
commit023d216b6aecbfafaa50542796200694218b01aa (patch)
tree0155a63bc6f0cd0b972d173bda18f18f3b79fd25
parent9aee75494d8d2f025b193dfd582c2adca1a08e98 (diff)
day 5
-rwxr-xr-xAdvent-of-Code-2022/aoc-5/build.sh5
-rw-r--r--Advent-of-Code-2022/aoc-5/input.txt512
-rw-r--r--Advent-of-Code-2022/aoc-5/main.c147
-rw-r--r--Advent-of-Code-2022/aoc-5/sample.txt9
4 files changed, 673 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-5/build.sh b/Advent-of-Code-2022/aoc-5/build.sh
new file mode 100755
index 0000000..30f495e
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-5/build.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+set -xe
+
+gcc -o main main.c -Wall -Wextra -pedantic
diff --git a/Advent-of-Code-2022/aoc-5/input.txt b/Advent-of-Code-2022/aoc-5/input.txt
new file mode 100644
index 0000000..968606f
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-5/input.txt
@@ -0,0 +1,512 @@
+ [M] [W] [M]
+ [L] [Q] [S] [C] [R]
+ [Q] [F] [F] [T] [N] [S]
+ [N] [V] [V] [H] [L] [J] [D]
+ [D] [D] [W] [P] [G] [R] [D] [F]
+[T] [T] [M] [G] [G] [Q] [N] [W] [L]
+[Z] [H] [F] [J] [D] [Z] [S] [H] [Q]
+[B] [V] [B] [T] [W] [V] [Z] [Z] [M]
+ 1 2 3 4 5 6 7 8 9
+
+move 1 from 7 to 4
+move 1 from 6 to 2
+move 5 from 9 to 4
+move 2 from 2 to 8
+move 2 from 2 to 6
+move 3 from 3 to 7
+move 3 from 7 to 1
+move 1 from 9 to 4
+move 4 from 7 to 3
+move 5 from 1 to 8
+move 1 from 1 to 2
+move 1 from 4 to 9
+move 4 from 5 to 6
+move 1 from 5 to 8
+move 3 from 2 to 4
+move 3 from 6 to 4
+move 3 from 5 to 9
+move 1 from 9 to 7
+move 1 from 9 to 8
+move 2 from 7 to 9
+move 1 from 7 to 9
+move 1 from 7 to 8
+move 2 from 9 to 8
+move 13 from 4 to 2
+move 2 from 6 to 1
+move 3 from 3 to 2
+move 9 from 2 to 7
+move 1 from 9 to 7
+move 5 from 7 to 8
+move 9 from 8 to 4
+move 2 from 7 to 1
+move 1 from 1 to 7
+move 2 from 3 to 2
+move 14 from 4 to 5
+move 3 from 8 to 4
+move 13 from 8 to 3
+move 2 from 7 to 1
+move 6 from 6 to 5
+move 1 from 6 to 9
+move 3 from 2 to 8
+move 1 from 7 to 8
+move 5 from 2 to 8
+move 4 from 4 to 8
+move 1 from 9 to 8
+move 1 from 7 to 1
+move 1 from 1 to 2
+move 7 from 3 to 2
+move 4 from 3 to 2
+move 2 from 5 to 3
+move 2 from 1 to 5
+move 5 from 8 to 7
+move 6 from 8 to 3
+move 11 from 5 to 8
+move 8 from 8 to 9
+move 5 from 7 to 8
+move 3 from 1 to 8
+move 5 from 3 to 8
+move 11 from 2 to 9
+move 1 from 3 to 5
+move 1 from 2 to 1
+move 1 from 2 to 7
+move 6 from 5 to 7
+move 19 from 9 to 7
+move 3 from 5 to 3
+move 1 from 5 to 4
+move 1 from 1 to 4
+move 1 from 9 to 8
+move 25 from 7 to 9
+move 2 from 4 to 1
+move 2 from 1 to 4
+move 2 from 4 to 7
+move 2 from 7 to 9
+move 5 from 3 to 1
+move 1 from 7 to 1
+move 9 from 9 to 5
+move 3 from 5 to 6
+move 9 from 9 to 1
+move 7 from 1 to 3
+move 6 from 8 to 9
+move 1 from 5 to 2
+move 10 from 9 to 2
+move 1 from 2 to 7
+move 5 from 9 to 8
+move 1 from 5 to 8
+move 5 from 3 to 8
+move 1 from 9 to 4
+move 4 from 3 to 6
+move 4 from 6 to 3
+move 3 from 1 to 4
+move 3 from 2 to 4
+move 3 from 5 to 8
+move 3 from 4 to 9
+move 1 from 7 to 3
+move 2 from 9 to 8
+move 4 from 2 to 5
+move 1 from 3 to 4
+move 1 from 9 to 3
+move 5 from 5 to 6
+move 7 from 8 to 5
+move 3 from 1 to 7
+move 6 from 5 to 8
+move 5 from 4 to 5
+move 3 from 3 to 2
+move 1 from 1 to 4
+move 19 from 8 to 1
+move 3 from 7 to 3
+move 4 from 2 to 9
+move 1 from 2 to 6
+move 7 from 6 to 4
+move 1 from 6 to 2
+move 2 from 1 to 3
+move 5 from 4 to 1
+move 1 from 6 to 2
+move 3 from 3 to 6
+move 12 from 1 to 2
+move 2 from 8 to 1
+move 14 from 2 to 4
+move 7 from 1 to 5
+move 10 from 4 to 6
+move 3 from 6 to 4
+move 1 from 8 to 4
+move 4 from 3 to 5
+move 1 from 2 to 3
+move 2 from 1 to 4
+move 17 from 5 to 3
+move 7 from 4 to 1
+move 1 from 9 to 4
+move 4 from 6 to 3
+move 5 from 4 to 8
+move 12 from 3 to 1
+move 6 from 3 to 5
+move 17 from 1 to 5
+move 2 from 1 to 9
+move 3 from 1 to 4
+move 7 from 8 to 2
+move 4 from 3 to 7
+move 1 from 1 to 8
+move 17 from 5 to 2
+move 11 from 2 to 8
+move 11 from 8 to 4
+move 11 from 2 to 4
+move 4 from 6 to 1
+move 4 from 1 to 3
+move 2 from 6 to 9
+move 3 from 7 to 8
+move 3 from 5 to 3
+move 23 from 4 to 3
+move 4 from 4 to 8
+move 1 from 7 to 4
+move 2 from 2 to 3
+move 6 from 3 to 2
+move 16 from 3 to 9
+move 2 from 5 to 8
+move 1 from 4 to 5
+move 2 from 5 to 9
+move 1 from 2 to 3
+move 1 from 3 to 8
+move 9 from 9 to 1
+move 6 from 3 to 8
+move 3 from 3 to 1
+move 18 from 8 to 9
+move 1 from 3 to 5
+move 5 from 1 to 4
+move 5 from 1 to 8
+move 3 from 4 to 1
+move 1 from 5 to 2
+move 2 from 4 to 8
+move 1 from 1 to 2
+move 5 from 2 to 7
+move 2 from 8 to 1
+move 2 from 2 to 6
+move 3 from 1 to 6
+move 3 from 9 to 6
+move 31 from 9 to 7
+move 26 from 7 to 8
+move 3 from 1 to 6
+move 22 from 8 to 4
+move 2 from 4 to 5
+move 4 from 6 to 5
+move 11 from 4 to 3
+move 9 from 4 to 6
+move 2 from 5 to 9
+move 4 from 7 to 1
+move 2 from 6 to 1
+move 1 from 5 to 3
+move 6 from 8 to 6
+move 8 from 6 to 2
+move 1 from 1 to 6
+move 3 from 1 to 3
+move 1 from 5 to 3
+move 1 from 5 to 9
+move 5 from 7 to 2
+move 2 from 9 to 6
+move 4 from 8 to 6
+move 1 from 7 to 2
+move 1 from 5 to 4
+move 12 from 3 to 4
+move 3 from 3 to 1
+move 3 from 6 to 8
+move 1 from 9 to 3
+move 6 from 2 to 6
+move 2 from 3 to 2
+move 10 from 2 to 7
+move 2 from 1 to 9
+move 2 from 1 to 6
+move 1 from 1 to 4
+move 9 from 7 to 9
+move 3 from 8 to 7
+move 7 from 4 to 8
+move 2 from 7 to 4
+move 4 from 8 to 1
+move 5 from 8 to 2
+move 3 from 1 to 3
+move 1 from 8 to 7
+move 3 from 3 to 7
+move 4 from 2 to 6
+move 8 from 4 to 2
+move 5 from 2 to 5
+move 11 from 9 to 7
+move 2 from 5 to 7
+move 16 from 7 to 8
+move 5 from 8 to 7
+move 1 from 4 to 3
+move 3 from 5 to 1
+move 11 from 6 to 5
+move 7 from 5 to 4
+move 5 from 7 to 4
+move 1 from 3 to 7
+move 2 from 5 to 4
+move 10 from 4 to 8
+move 14 from 6 to 3
+move 1 from 5 to 9
+move 1 from 6 to 5
+move 2 from 2 to 9
+move 2 from 1 to 3
+move 2 from 5 to 3
+move 2 from 7 to 6
+move 2 from 1 to 4
+move 1 from 2 to 3
+move 19 from 3 to 6
+move 3 from 9 to 2
+move 4 from 2 to 6
+move 6 from 6 to 7
+move 13 from 6 to 2
+move 14 from 8 to 1
+move 6 from 4 to 3
+move 5 from 7 to 8
+move 3 from 6 to 3
+move 2 from 8 to 2
+move 2 from 6 to 8
+move 4 from 1 to 8
+move 13 from 8 to 4
+move 10 from 4 to 7
+move 1 from 4 to 5
+move 1 from 5 to 1
+move 3 from 6 to 5
+move 3 from 8 to 9
+move 9 from 3 to 1
+move 3 from 5 to 8
+move 3 from 9 to 6
+move 3 from 8 to 7
+move 1 from 6 to 9
+move 1 from 9 to 4
+move 9 from 2 to 8
+move 2 from 2 to 6
+move 14 from 7 to 1
+move 31 from 1 to 5
+move 3 from 1 to 7
+move 4 from 6 to 8
+move 24 from 5 to 3
+move 2 from 8 to 1
+move 1 from 8 to 5
+move 2 from 1 to 7
+move 3 from 7 to 6
+move 6 from 8 to 6
+move 2 from 4 to 2
+move 1 from 4 to 3
+move 2 from 2 to 7
+move 6 from 6 to 7
+move 4 from 8 to 6
+move 7 from 6 to 2
+move 12 from 7 to 5
+move 4 from 2 to 8
+move 1 from 2 to 4
+move 1 from 4 to 6
+move 1 from 6 to 7
+move 1 from 7 to 3
+move 3 from 3 to 8
+move 17 from 3 to 5
+move 4 from 3 to 6
+move 35 from 5 to 3
+move 2 from 2 to 6
+move 1 from 5 to 9
+move 9 from 3 to 7
+move 6 from 8 to 1
+move 4 from 2 to 6
+move 4 from 6 to 9
+move 20 from 3 to 9
+move 22 from 9 to 7
+move 1 from 8 to 6
+move 29 from 7 to 5
+move 4 from 6 to 8
+move 6 from 1 to 8
+move 2 from 7 to 3
+move 1 from 6 to 5
+move 2 from 3 to 9
+move 1 from 9 to 3
+move 4 from 5 to 6
+move 18 from 5 to 1
+move 7 from 3 to 1
+move 1 from 3 to 6
+move 3 from 5 to 1
+move 1 from 3 to 9
+move 4 from 5 to 2
+move 10 from 8 to 7
+move 2 from 9 to 3
+move 1 from 3 to 5
+move 21 from 1 to 9
+move 1 from 3 to 2
+move 1 from 2 to 9
+move 15 from 9 to 3
+move 4 from 7 to 1
+move 2 from 6 to 1
+move 7 from 9 to 1
+move 1 from 5 to 4
+move 1 from 4 to 6
+move 6 from 3 to 9
+move 3 from 6 to 5
+move 19 from 1 to 6
+move 8 from 3 to 6
+move 1 from 3 to 7
+move 20 from 6 to 7
+move 1 from 2 to 6
+move 6 from 9 to 8
+move 2 from 9 to 4
+move 1 from 1 to 3
+move 1 from 2 to 9
+move 3 from 5 to 6
+move 2 from 7 to 3
+move 2 from 9 to 7
+move 1 from 4 to 8
+move 4 from 8 to 9
+move 4 from 7 to 1
+move 2 from 1 to 7
+move 1 from 3 to 2
+move 2 from 8 to 9
+move 6 from 6 to 2
+move 1 from 8 to 1
+move 1 from 5 to 7
+move 4 from 2 to 9
+move 1 from 3 to 5
+move 5 from 6 to 1
+move 1 from 4 to 2
+move 1 from 9 to 6
+move 1 from 9 to 6
+move 4 from 6 to 3
+move 7 from 9 to 7
+move 8 from 7 to 2
+move 1 from 5 to 8
+move 5 from 2 to 9
+move 3 from 2 to 5
+move 6 from 1 to 3
+move 17 from 7 to 6
+move 1 from 8 to 2
+move 2 from 1 to 7
+move 5 from 9 to 4
+move 4 from 3 to 8
+move 3 from 4 to 1
+move 1 from 9 to 2
+move 4 from 2 to 1
+move 1 from 8 to 4
+move 1 from 5 to 9
+move 1 from 2 to 3
+move 3 from 3 to 2
+move 10 from 7 to 6
+move 3 from 4 to 1
+move 5 from 3 to 2
+move 4 from 1 to 5
+move 3 from 8 to 6
+move 12 from 6 to 4
+move 1 from 9 to 3
+move 1 from 5 to 2
+move 3 from 1 to 6
+move 12 from 6 to 8
+move 3 from 1 to 5
+move 2 from 4 to 3
+move 5 from 8 to 7
+move 7 from 5 to 3
+move 3 from 7 to 9
+move 1 from 5 to 8
+move 5 from 3 to 7
+move 10 from 6 to 5
+move 2 from 7 to 5
+move 8 from 2 to 9
+move 5 from 3 to 9
+move 9 from 5 to 1
+move 5 from 7 to 4
+move 15 from 9 to 5
+move 1 from 2 to 5
+move 1 from 8 to 5
+move 6 from 4 to 1
+move 2 from 2 to 9
+move 18 from 5 to 8
+move 18 from 8 to 3
+move 16 from 3 to 4
+move 3 from 5 to 8
+move 1 from 9 to 2
+move 3 from 1 to 7
+move 3 from 8 to 2
+move 3 from 7 to 9
+move 2 from 3 to 4
+move 3 from 9 to 8
+move 11 from 1 to 6
+move 2 from 9 to 4
+move 3 from 6 to 9
+move 8 from 6 to 4
+move 26 from 4 to 7
+move 1 from 2 to 5
+move 1 from 5 to 2
+move 3 from 9 to 3
+move 21 from 7 to 6
+move 4 from 2 to 4
+move 1 from 2 to 3
+move 5 from 7 to 6
+move 8 from 8 to 1
+move 1 from 3 to 7
+move 9 from 1 to 4
+move 1 from 7 to 4
+move 20 from 4 to 7
+move 1 from 8 to 5
+move 2 from 4 to 8
+move 1 from 4 to 9
+move 3 from 8 to 9
+move 1 from 5 to 8
+move 2 from 3 to 1
+move 4 from 7 to 8
+move 3 from 7 to 5
+move 1 from 1 to 7
+move 4 from 8 to 3
+move 3 from 5 to 6
+move 1 from 8 to 4
+move 1 from 1 to 8
+move 28 from 6 to 4
+move 1 from 6 to 1
+move 2 from 7 to 8
+move 1 from 8 to 7
+move 1 from 8 to 1
+move 2 from 1 to 9
+move 3 from 9 to 3
+move 12 from 7 to 5
+move 7 from 3 to 1
+move 1 from 3 to 6
+move 26 from 4 to 9
+move 1 from 6 to 3
+move 1 from 3 to 6
+move 1 from 8 to 6
+move 1 from 7 to 8
+move 1 from 6 to 3
+move 3 from 1 to 3
+move 5 from 4 to 3
+move 28 from 9 to 4
+move 2 from 1 to 5
+move 22 from 4 to 1
+move 3 from 5 to 3
+move 5 from 5 to 7
+move 10 from 1 to 6
+move 1 from 5 to 2
+move 3 from 5 to 3
+move 2 from 5 to 9
+move 3 from 9 to 7
+move 2 from 4 to 5
+move 1 from 5 to 4
+move 4 from 3 to 8
+move 1 from 5 to 7
+move 9 from 6 to 5
+move 1 from 7 to 6
+move 1 from 6 to 5
+move 2 from 6 to 9
+move 3 from 5 to 1
+move 13 from 1 to 3
+move 7 from 7 to 5
+move 1 from 2 to 9
+move 3 from 8 to 2
+move 1 from 7 to 2
+move 3 from 4 to 3
+move 19 from 3 to 8
+move 5 from 3 to 7
+move 1 from 7 to 1
+move 19 from 8 to 6
+move 5 from 1 to 4
+move 5 from 5 to 2
+move 2 from 2 to 7
+move 3 from 4 to 1
+move 6 from 5 to 7
+move 2 from 8 to 7
+move 2 from 2 to 7
+move 3 from 3 to 5
+move 5 from 7 to 6
+move 6 from 6 to 1
+move 2 from 5 to 1
+move 2 from 4 to 3
+move 1 from 5 to 8
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);
+}
diff --git a/Advent-of-Code-2022/aoc-5/sample.txt b/Advent-of-Code-2022/aoc-5/sample.txt
new file mode 100644
index 0000000..42ef47f
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-5/sample.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