day 20
[advent21.git] / 18a.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5
6 struct number {
7 int depth;
8 int number;
9 };
10
11 int parse_number(struct number numbers[])
12 {
13 int ns = 0;
14 int depth = 0;
15 int c = getchar();
16 while ((c = getchar()) != '\n') {
17 switch(c) {
18 case '[':
19 depth++;
20 break;
21 case ']':
22 depth--;
23 break;
24 case ',':
25 break;
26 default:
27 if (isdigit(c))
28 numbers[ns++] = (struct number){.depth=depth, .number=c-'0'};
29 else
30 printf("halp\n");
31 break;
32 }
33 }
34 return ns;
35 }
36
37 void print_number(struct number n[], int ns)
38 {
39 int olddepth = 0;
40 for (int i = 0; i<ns; i++) {
41 if (n[i].depth > olddepth)
42 printf("[");
43 else if (n[i].depth < olddepth)
44 printf("]");
45 printf("%d ", n[i].number);
46 olddepth = n[i].depth;
47 }
48 }
49
50 int main ()
51 {
52 struct number n[100];
53 int ns = parse_number(n);
54 print_number(n, ns);
55 }