]> www.fi.muni.cz Git - aoc.git/blob - 2020/13.c
Mods from 2020
[aoc.git] / 2020 / 13.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #define MAXLINE 1000
6 #define MAXBAGS 1000
7
8 struct bag {
9         char *color;
10         struct baglist *above;
11 };
12
13 struct baglist {
14         struct bag *bag,
15         struct baglist *next;
16 };
17         
18 char *bagnames[MAXBAGS];
19
20 int main()
21 {
22         char buffer[MAXLINE];
23
24         while (fgets(buffer, MAXLINE, stdin)) {
25                 char *p;
26                 p = strstr(buffer, " bag");
27                 *p = 0;
28                 printf("bag: %s\n", buffer);
29                 p = strstr(p+1, " contain ") + strlen(" contain");
30
31                 while (p) {
32                         if (*p == ' ')
33                                 p++;
34                         if (*p == 'n') {
35                                 printf("\tno other\n");
36                                 break;
37                         }
38                         int num = atoi(p);
39                         char *bag = p = strchr(p, ' ') + 1;
40                         p = strstr(p, " bag");
41                         *p = 0;
42                         p += strlen(" bag");
43                         printf("\t%d, %s.\n", num, bag);
44                         p = strchr(p, ' ');
45                 }
46         }
47 }