day 23 optimize again
[aoc_eblake.git] / 2016 / advent7.c
blob121e294cbaa33bc3824b36c024038a041a24d0dc
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include <ctype.h>
9 bool
10 check (const char *s)
12 int len = strlen (s);
13 printf ("checking %s\n", s);
14 for (int i = 0; i < len - 3; i++)
15 if (s[i] != s[i + 1] && s[i + 1] == s[i + 2] && s[i] == s[i + 3])
16 return true;
17 printf ("no abba\n");
18 return false;
21 char *
22 find (char *s)
24 while (*s && s[1] && s[2]) {
25 if (s[0] != s[1] && s[0] == s[2])
26 return s;
27 s++;
29 return NULL;
32 bool
33 match (const char *h, const char *patt)
35 char n[4] = { patt[1], patt[0], patt[1] };
36 printf ("looking for %s in %s\n", n, h);
37 return strstr (h, n);
40 int main(int argc, char **argv)
42 ssize_t nread;
43 size_t len = 0;
44 char *line = NULL;
45 int count1 = 0, count2 = 0;
46 char super[80], hyper[80];
47 while ((nread = getline(&line, &len, stdin)) >= 0) {
48 line[nread - 1] = '\0';
49 char *p = line;
50 printf ("parsing %s\n", line);
51 super[0] = hyper[0] = '\0';
52 char *s = super, *h = hyper;
53 char goal = '[';
54 while (*p) {
55 char *q = strchrnul (p, goal);
56 if (goal == '[')
57 s += sprintf (s, "%.*s[]", (int) (q - p), p);
58 else
59 h += sprintf (h, "%.*s ", (int) (q - p), p);
60 p = q + !!*q;
61 goal ^= 6; // cute ascii toggle between [ and ]
63 if (check (s = super) && ! check (hyper))
64 count1++;
65 while ((s = find (s)))
66 if (match (hyper, s++)) {
67 count2++;
68 break;
71 printf ("final count: TLS %d, SSL %d\n", count1, count2);
72 return 0;