]> www.fi.muni.cz Git - aoc.git/blobdiff - 2020/13.c
Mods from 2020
[aoc.git] / 2020 / 13.c
diff --git a/2020/13.c b/2020/13.c
new file mode 100644 (file)
index 0000000..a8b92d5
--- /dev/null
+++ b/2020/13.c
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#define MAXLINE 1000
+#define MAXBAGS 1000
+
+struct bag {
+       char *color;
+       struct baglist *above;
+};
+
+struct baglist {
+       struct bag *bag,
+       struct baglist *next;
+};
+       
+char *bagnames[MAXBAGS];
+
+int main()
+{
+       char buffer[MAXLINE];
+
+       while (fgets(buffer, MAXLINE, stdin)) {
+               char *p;
+               p = strstr(buffer, " bag");
+               *p = 0;
+               printf("bag: %s\n", buffer);
+               p = strstr(p+1, " contain ") + strlen(" contain");
+
+               while (p) {
+                       if (*p == ' ')
+                               p++;
+                       if (*p == 'n') {
+                               printf("\tno other\n");
+                               break;
+                       }
+                       int num = atoi(p);
+                       char *bag = p = strchr(p, ' ') + 1;
+                       p = strstr(p, " bag");
+                       *p = 0;
+                       p += strlen(" bag");
+                       printf("\t%d, %s.\n", num, bag);
+                       p = strchr(p, ' ');
+               }
+       }
+}