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
75
76
77
78
79
80
81
82
83
84
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define WHOLE \
fseek(fp, 0, SEEK_END); \
long fsize = ftell(fp); \
fseek(fp, 0, SEEK_SET); \
char *data = malloc(fsize + 1); \
fread(data, 1, fsize, fp); \
data[fsize] = 0;
#define SIZE 100
#define MULTI (SIZE + 10) // idk
#define LINE (SIZE + 1)
#define GET(x, y) image[(y) * (SIZE + MULTI) + (x)]
static void solve(FILE *fp)
{
WHOLE;
char buff[(SIZE + MULTI) * (SIZE + MULTI)];
char image[(SIZE + MULTI) * (SIZE + MULTI)];
for (int y = 0; y < SIZE; y++)
for (int x = 0; x < SIZE; x++)
buff[(y + MULTI / 2) * (SIZE + MULTI) + (x + MULTI / 2)] =
data[514 + (y * LINE + x)];
int offset = 1;
aah:;
memset(image, offset % 2 == 0 ? '#' : '.', sizeof(image)); // Due to alg[0]
for (int y = MULTI / 2 - offset + 1; y < SIZE + MULTI / 2 + offset - 1; y++)
for (int x = MULTI / 2 - offset + 1; x < SIZE + MULTI / 2 + offset - 1; x++)
image[y * (SIZE + MULTI) + x] = buff[y * (SIZE + MULTI) + x];
for (int y = MULTI / 2 - offset; y < SIZE + MULTI / 2 + offset; y++) {
for (int x = MULTI / 2 - offset; x < SIZE + MULTI / 2 + offset; x++) {
int algorithm = ((GET(x - 1, y - 1) == '#') << 8) |
((GET(x - 0, y - 1) == '#') << 7) |
((GET(x + 1, y - 1) == '#') << 6) |
((GET(x - 1, y - 0) == '#') << 5) |
((GET(x - 0, y - 0) == '#') << 4) |
((GET(x + 1, y - 0) == '#') << 3) |
((GET(x - 1, y + 1) == '#') << 2) |
((GET(x - 0, y + 1) == '#') << 1) |
((GET(x + 1, y + 1) == '#') << 0);
buff[y * (SIZE + MULTI) + x] = data[algorithm];
}
}
if (offset == 2 || offset == 50) {
int res = 0;
for (unsigned int i = 0; i < sizeof(buff); i++)
if (buff[i] == '#')
res++;
printf("%d\n", res);
}
if (offset == 50) {
free(data);
return;
}
offset++;
goto aah;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
clock_t tic = clock();
solve(fp);
clock_t toc = clock();
printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
fclose(fp);
return 0;
}
|