-
Notifications
You must be signed in to change notification settings - Fork 2
/
17.c
204 lines (181 loc) · 5.17 KB
/
17.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "inputs/17.h"
#define GRIDSIZE 24
static void err(int status, char *message) {
fputs(message, stderr);
exit(status);
}
typedef enum {
STATE_ACTIVE = 1,
STATE_INACTIVE = 0,
} state_t;
struct grid {
int8_t* neighbor_counts;
char* values;
};
typedef struct grid grid_t;
static grid_t
read_input(void) {
const unsigned char *s = input;
// assume high enough values to fit our infinite grid
grid_t g;
g.values = (char*)calloc(GRIDSIZE * GRIDSIZE * GRIDSIZE * GRIDSIZE, sizeof(char));
if (!g.values) {
err(EXIT_FAILURE, "could not allocate grid values");
}
g.neighbor_counts =
(int8_t*)malloc(GRIDSIZE * GRIDSIZE * GRIDSIZE * GRIDSIZE * sizeof(int8_t));
if (!g.neighbor_counts) {
err(EXIT_FAILURE, "could not allocate neighbor counts");
}
const int32_t c = (GRIDSIZE / 2) - 1; // centre point
int32_t x;
int32_t y = c;
const int32_t z = c;
const int32_t w = c;
int32_t offset_2d = (w * GRIDSIZE * GRIDSIZE * GRIDSIZE)
+ (z * GRIDSIZE * GRIDSIZE);
while (*s != '\0') {
x = c;
while (*s != '\n' && *s != '\0') {
g.values[offset_2d
+ (y * GRIDSIZE) + x] =
(*s++ == '#') ? STATE_ACTIVE : STATE_INACTIVE;
x++;
}
if (*s == '\n') {
s++;
}
y++;
}
return g;
}
static void
print_grid(grid_t g) {
int32_t count = 0;
for (int32_t z = 0; z < GRIDSIZE; z++) {
printf("z=%d\n", z - 100 / 2 - 2);
for (int32_t y = 0; y < GRIDSIZE; y++) {
for (int32_t x = 0; x < GRIDSIZE; x++) {
if (g.values[(GRIDSIZE * GRIDSIZE * z) + (GRIDSIZE * y) + x] ==
STATE_ACTIVE) {
printf("#");
count++;
} else {
printf(".");
}
}
printf("\n");
}
printf("\n");
}
printf("count = %d\n", count);
}
static void
add_one_to_all_neighbours(grid_t* restrict g,
const int32_t pos_x,
const int32_t pos_y,
const int32_t pos_z,
const int32_t pos_w) {
const int32_t idx_self = (pos_w * GRIDSIZE * GRIDSIZE * GRIDSIZE)
+ (pos_z * GRIDSIZE * GRIDSIZE)
+ (pos_y * GRIDSIZE)
+ pos_x;
for (int8_t w = pos_w - 1; w <= pos_w + 1; w++) {
for (int8_t z = pos_z - 1; z <= pos_z + 1; z++) {
for (int8_t y = pos_y - 1; y <= pos_y + 1; y++) {
for (int8_t x = pos_x - 1; x <= pos_x + 1; x++) {
int32_t idx = (w * GRIDSIZE * GRIDSIZE * GRIDSIZE)
+ (z * GRIDSIZE * GRIDSIZE)
+ (y * GRIDSIZE)
+ x;
g->neighbor_counts[idx] += 1;;
}
}
}
}
// deduct 1 from self since above loop adds 1 to self
// this saves a branch in the inner loop
g->neighbor_counts[idx_self] -= 1;
}
static void
update_neighbor_counts(grid_t* restrict g) {
memset(g->neighbor_counts, 0,
GRIDSIZE * GRIDSIZE * GRIDSIZE * GRIDSIZE * sizeof(int8_t));
for (int8_t w = 1; w < GRIDSIZE - 1; w++) {
for (int8_t z = 1; z < GRIDSIZE - 1; z++) {
for (int8_t y = 1; y < GRIDSIZE - 1; y++) {
for (int8_t x = 1; x < GRIDSIZE - 1; x++) {
const int32_t idx = (w * GRIDSIZE * GRIDSIZE * GRIDSIZE)
+ (z * GRIDSIZE * GRIDSIZE)
+ (y * GRIDSIZE)
+ x;
if (g->values[idx] == STATE_ACTIVE) {
// add one to all neighbors
add_one_to_all_neighbours(g, x, y, z, w);
}
}
}
}
}
}
static int32_t
transmute_grid(grid_t* restrict g) {
update_neighbor_counts(g);
int32_t count = 0;
for (int32_t w = 1; w < GRIDSIZE - 1; w++) {
for (int32_t z = 1; z < GRIDSIZE - 1; z++) {
for (int32_t y = 1; y < GRIDSIZE - 1; y++) {
for (int32_t x = 1; x < GRIDSIZE - 1; x++) {
const int32_t idx = (w * GRIDSIZE * GRIDSIZE * GRIDSIZE)
+ (z * GRIDSIZE * GRIDSIZE)
+ (y * GRIDSIZE)
+ x;
const int8_t active_neighbor_count = g->neighbor_counts[idx];
switch (g->values[idx]) {
case STATE_ACTIVE:
if (active_neighbor_count < 2 || active_neighbor_count > 3) {
g->values[idx] = STATE_INACTIVE;
} else {
count++;
}
break;
default:
case STATE_INACTIVE:
// If a cube is inactive but exactly 3 of its neighbors
// are active, the cube becomes active.
if (active_neighbor_count == 3) {
g->values[idx] = STATE_ACTIVE;
count++;
}
break;
}
}
}
}
}
return count;
}
// TODO: Grow grid dynamically, as this should also improve performance by
// reducing loop iterations
int day17(void) {
grid_t g = read_input();
int32_t count = 0;
for (int32_t i = 0; i < 6; i++) {
count = transmute_grid(&g);
#ifdef STEP
getchar();
printf("Step %d\n", i + 1);
print_grid(g);
#endif
}
printf("%d\n", count);
assert(count == 1380);
free(g.neighbor_counts);
free(g.values);
return 0;
}