day 20
[advent21.git] / 18a.c
diff --git a/18a.c b/18a.c
new file mode 100644 (file)
index 0000000..719c3f0
--- /dev/null
+++ b/18a.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+struct number {
+       int depth;
+       int number;
+};
+
+int parse_number(struct number numbers[])
+{
+       int ns = 0;
+       int depth = 0;
+       int c = getchar();
+       while ((c = getchar()) != '\n') {
+               switch(c) {
+               case '[':
+                       depth++;
+                       break;
+               case ']':
+                       depth--;
+                       break;
+               case ',':
+                       break;
+               default:
+                       if (isdigit(c))
+                               numbers[ns++] = (struct number){.depth=depth, .number=c-'0'};
+                       else
+                               printf("halp\n");
+                       break;
+               }
+       }
+       return ns;
+}
+
+void print_number(struct number n[], int ns)
+{
+       int olddepth = 0;
+       for (int i = 0; i<ns; i++) {
+               if (n[i].depth > olddepth)
+                       printf("[");
+               else if (n[i].depth < olddepth)
+                       printf("]");
+               printf("%d ", n[i].number);
+               olddepth = n[i].depth;
+       }
+}
+
+int main ()
+{
+       struct number n[100];
+       int ns = parse_number(n);
+       print_number(n, ns);
+}