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
85
86
87
88
89
90
91
92
93
94
|
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10000
#define X_MAX 99
#define Y_MAX 97
// Quite magical, isn't it?
#define ADJACENT(x, y, p) \
(x != 0 && y != 0 ? *(p - X_MAX - 1) == '#' : 0) + (y != 0 ? *(p - X_MAX) == '#' : 0) + \
(x != X_MAX && y != 0 ? *(p - X_MAX + 1) == '#' : 0) + \
(x != 0 ? *(p - 1) == '#' : 0) + (x != X_MAX ? *(p + 1) == '#' : 0) + \
(x != 0 && y != Y_MAX ? *(p + X_MAX - 1) == '#' : 0) + \
(y != Y_MAX ? *(p + X_MAX) == '#' : 0) + \
(x != X_MAX && y != Y_MAX ? *(p + X_MAX + 1) == '#' : 0);
int count(char *buf1, char *buf2)
{
int x = 0, y = 0;
for (char *p = buf1; *p; p++) {
if (*p == '.') {
x++;
buf2[p - buf1] = '.';
continue;
}
if (*p == '\n') {
x = 0;
y++;
buf2[p - buf1] = '\n';
continue;
}
int cnt = ADJACENT(x, y, p);
if (*p == 'L' && !cnt) {
buf2[p - buf1] = '#';
} else if (*p == '#' && cnt >= 4) {
buf2[p - buf1] = 'L';
} else {
buf2[p - buf1] = *p;
}
x++;
}
if (memcmp(buf1, buf2, SIZE)) {
return count(buf2, buf1); // Swap buffers
} else {
int cnt = 0;
for (char *p = buf1; *p; p++) {
if (*p == '#')
cnt++;
}
return cnt;
}
}
int part_one(char *data)
{
int res = 0;
char buf1[SIZE] = { 0 };
memcpy(buf1, data, SIZE);
char buf2[SIZE] = { 0 };
memcpy(buf2, data, SIZE);
return count(buf1, buf2);
}
int part_two(char *data)
{
int res = 0;
return res;
}
int main(int argc, char *argv[])
{
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
char buf[SIZE] = { 0 };
fread(buf, SIZE, 1, fp);
printf("%d\n", part_one(buf));
printf("%d\n", part_two(buf));
fclose(fp);
return 0;
}
|