day 11 part 1
[aoc_eblake.git] / 2017 / advent4.c
blob675aff1584374866ec91aabf8fba524109dd9cee
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
6 int sorter(const void *a, const void *b) {
7 return *(unsigned char *)a - *(unsigned char *)b;
10 int main(void) {
11 ssize_t nread;
12 size_t len = 0;
13 char *line = NULL;
14 int sum = 0;
15 while ((nread = getline(&line, &len, stdin)) >= 0) {
16 char *cur = line;
17 char *space;
18 line[nread - 1] = ' ';
19 sum++;
20 #if 1 // part 2
21 while ((space = memchr(cur, ' ', line + nread - cur))) {
22 qsort(cur, space - cur, 1, sorter);
23 cur = space + 1;
25 cur = line;
26 #endif
27 while ((space = strchr(cur, ' '))) {
28 if (space - line == nread - 1)
29 break;
30 char *candidate = space;
31 while ((candidate = memmem(candidate + 1, line + nread - candidate - 1,
32 cur, space - cur + 1))) {
33 if (candidate[-1] == ' ') {
34 goto skip;
37 cur = space + 1;
39 continue;
40 skip:
41 sum--;
43 free (line);
44 printf("%d\n", sum);