day 25 optimize and improve heuristics
[aoc_eblake.git] / 2016 / advent9.c
blob06f5beaead2957016a36b2271bb342992d57c170
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 #define COLS 50
10 #define ROWS 6
11 bool grid[ROWS][COLS];
12 bool row[COLS];
13 bool col[ROWS];
15 long long
16 grow (const char *str, int n)
18 long long count = 0;
19 const char *p = str;
20 while (p < str + n) {
21 if (*p++ == '(') {
22 int x, y, l;
23 if (sscanf (p, "%dx%d)%n", &x, &y, &l) != 2)
24 exit (1);
25 p += l;
26 count += y * grow (p, x);
27 p += x;
28 } else {
29 count++;
32 return count;
35 int main(int argc, char **argv)
37 long long count = 0;
38 int c;
39 while (!isspace (c = getchar ())) {
40 if (c == '(') {
41 int x, y;
42 if (scanf ("%dx%d)", &x, &y) != 2)
43 return 1;
44 char *s = malloc (x);
45 fread (s, 1, x, stdin);
46 count += y * grow (s, x);
47 free (s);
48 } else
49 count++;
51 printf ("decompressed length: %lld\n", count);
52 return 0;