day 15 golfed m4, 829 effective bytes
[aoc_eblake.git] / 2016 / advent8.c
blob069890be9927b30fc7948fbccacb0dff8ae0f6e3
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 int main(int argc, char **argv)
17 ssize_t nread;
18 size_t len = 0;
19 char *line = NULL;
20 int count = 0;
21 int i, j;
22 while ((nread = getline(&line, &len, stdin)) >= 0) {
23 char *end;
24 int x, y;
25 count++;
26 if (!strncmp (line, "rect ", 5)) {
27 x = strtol (line + 5, &end, 10);
28 y = strtol (end + 1, NULL, 10);
29 for (i = 0; i < y; i++)
30 for (j = 0; j < x; j++)
31 grid[i][j] = true;
32 } else if (!strncmp (line, "rotate row y=", 13)) {
33 x = strtol (line + 13, &end, 10);
34 y = strtol (end + 3, NULL, 10);
35 for (i = 0; i < COLS; i++)
36 row[i] = grid[x][i];
37 for (i = 0; i < COLS; i++)
38 grid[x][(i + y) % COLS] = row[i];
39 } else { // rotate column x=
40 x = strtol (line + 16, &end, 10);
41 y = strtol (end + 3, NULL, 10);
42 for (i = 0; i < ROWS; i++)
43 col[i] = grid[i][x];
44 for (i = 0; i < ROWS; i++)
45 grid[(i + y) % ROWS][x] = col[i];
48 int lit = 0;
49 for (i = 0; i < ROWS; i++) {
50 for (j = 0; j < COLS; j++) {
51 putchar (grid[i][j] ? '#' : ' ');
52 if (grid[i][j])
53 lit++;
55 putchar ('\n');
57 printf ("after %d instructions, %d pixels are lit\n", count, lit);
58 return 0;