7 enum cellstatus
{active
, inactive
};
9 //char *inp[] = {".#.","..#","###"};
25 #define mapel(x, y, z) map[(z)*ymax*xmax+(y)*xmax+(x)]
27 enum cellstatus
oldstatus(int x
, int y
, int z
)
29 if(x
<0 || y
<0 || z
<0 || x
>= xmax
|| y
>= ymax
|| z
>= zmax
)
31 return (enum cellstatus
)((mapel(x
, y
, z
) >> 8) & 0xff);
34 enum cellstatus
newstatus(int x
, int y
, int z
)
36 if(x
<0 || y
<0 || z
<0 || x
>= xmax
|| y
>= ymax
|| z
>= zmax
)
38 return (enum cellstatus
)(mapel(x
, y
, z
) & 0xff);
43 //Move the old to the leftmost byte
44 for (int z
= 0; z
<zmax
; z
++)
45 for (int y
= 0; y
<ymax
; y
++)
46 for (int x
= 0; x
<xmax
; x
++)
49 for (int z
= 0; z
<zmax
; z
++) {
50 for (int y
= 0; y
<ymax
; y
++) {
51 for (int x
= 0; x
<xmax
; x
++) {
53 for (int dz
= -1; dz
<=1; dz
++) {
54 for (int dy
= -1; dy
<=1; dy
++) {
55 for (int dx
= -1; dx
<=1; dx
++) {
56 if (dx
== 0 && dy
== 0 && dz
== 0)
58 if (oldstatus(x
+dx
, y
+dy
, z
+dz
) == active
)
63 // fprintf(stderr, "x=%u, y=%u, z=%u, count: %u\n", x, y, z, count);
64 switch (oldstatus(x
, y
, z
)) {
67 |= count
== 2 || count
== 3
83 //Determine dimensions
84 int xorig
= strlen(inp
[0]);
85 int yorig
= sizeof(inp
)/sizeof(char *);
90 fprintf(stderr
, "Total size: %ux%ux%u, %u elements, %u steps\n",
91 xmax
, ymax
, zmax
, xmax
*ymax
*zmax
, steps
);
92 map
= malloc(sizeof(uint16_t)*xmax
*ymax
*zmax
);
95 for(int z
= 0; z
<zmax
; z
++)
96 for(int y
= 0; y
<ymax
; y
++)
97 for(int x
= 0; x
<xmax
; x
++)
98 mapel(x
, y
, z
) = inactive
;
99 for(int y
= 0; y
<yorig
; y
++)
100 for(int x
= 0; x
<xorig
; x
++) {
101 mapel(x
+steps
, y
+steps
, steps
) =
102 inp
[y
][x
] == '#' ? active
: inactive
;
105 for(int i
= 0; i
<steps
; i
++) {
106 fprintf(stderr
, "step: %d\n", i
);
111 for(int z
= 0; z
<zmax
; z
++)
112 for(int y
= 0; y
<ymax
; y
++)
113 for(int x
= 0; x
<xmax
; x
++)
114 if (newstatus(x
, y
, z
) == active
)
117 printf("%d\n", count
);