1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
+
+
+
|
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
struct LampGrid {
unsigned lit:1;
unsigned bright:31;
};
static void gridchange(bool *grid, int v,
static void gridchange(struct LampGrid *grid, int v,
unsigned r1, unsigned c1, unsigned r2, unsigned c2) {
unsigned firstrow = (r1 < r2) ? r1 : r2;
unsigned lastrow = (r1 < r2) ? r2 : r1;
unsigned firstcol = (c1 < c2) ? c1 : c2;
unsigned lastcol = (c1 < c2) ? c2 : c1;
for (unsigned row = firstrow; row <= lastrow; row++) {
for (unsigned col = firstcol; col <= lastcol; col++) {
grid[1000*row + col] = (v < 0) ? !grid[1000*row + col] : !!v;
size_t index = 1000*row + col;
switch (v) {
default: fprintf(stderr, "bad value detected\n");
exit(EXIT_FAILURE);
// break; // never reached
case -1: grid[index].lit = 0;
if (grid[index].bright > 0) grid[index].bright -= 1;
break;
case 1: grid[index].lit = 1;
grid[index].bright += 1;
break;
case 2: grid[index].lit = 1 - grid[index].lit;
grid[index].bright += 2;
break;
}
}
}
}
void aoc201506(char *data, size_t len) {
(void)len; // unused argument
bool *grid_ptr = calloc(1000 * 1000, 1);
struct LampGrid *grid_ptr = calloc(1000 * 1000, sizeof *grid_ptr);
char *line = strtok(data, "\n");
while (line) {
unsigned x1, x2, y1, y2;
char *err = line;
while (!isdigit((unsigned char)*err)) err++;
y1 = strtoul(err, &err, 10); err++;
x1 = strtoul(err, &err, 10); while (!isdigit((unsigned char)*err)) err++;
y2 = strtoul(err, &err, 10); err++;
x2 = strtoul(err, &err, 10);
if (line[6] == 'n') gridchange(grid_ptr, 1, y1, x1, y2, x2);
if (line[6] == 'f') gridchange(grid_ptr, 0, y1, x1, y2, x2);
if (line[6] == ' ') gridchange(grid_ptr, -1, y1, x1, y2, x2);
if (line[6] == 'f') gridchange(grid_ptr, -1, y1, x1, y2, x2);
if (line[6] == ' ') gridchange(grid_ptr, 2, y1, x1, y2, x2);
line = strtok(NULL, "\n");
}
unsigned lit = 0;
unsigned lit = 0, brightness = 0;
for (unsigned row = 0; row < 1000; row++) {
for (unsigned col = 0; col < 1000; col++) {
lit += grid_ptr[1000*row + col];
lit += grid_ptr[1000*row + col].lit;
brightness += grid_ptr[1000*row + col].bright;
}
}
free(grid_ptr);
printf("After following the instructions, there are %u lights lit.\n", lit);
printf("Total brightness is {%u}.\n", brightness);
}
void aoc201505(char *data, size_t len) {
(void)len; // unused argument
unsigned nice = 0, nice2 = 0;
char *line = strtok(data, "\n"), *curr;
while (line) {
|