4 char cmap
[] = { [')']='(', [']']='[', ['}']='{', ['>']='<' };
5 long rmap
[] = { ['(']=1, ['[']=2, ['{']=3, ['<']=4 };
7 void parse_line(const char *buf
, long scores
[], int *nscores
)
12 for (const char *p
= buf
; ; p
++) {
25 if (stack
[--sp
] != cmap
[(int)*p
])
33 score
= score
*5 + rmap
[(int)stack
[--sp
]];
34 scores
[(*nscores
)++] = score
;
40 int longcmp(const void *l
, const void *r
)
42 return *(const long *)l
-*(const long *)r
< 0 ? -1 : 1;
49 long scores
[100] = {0};
51 while (getline(&buf
, &len
, stdin
) != -1)
52 parse_line(buf
, scores
, &nscores
);
53 qsort(scores
, nscores
, sizeof(long), &longcmp
);
54 printf("%ld\n", scores
[nscores
/2]);