5 struct point
{int x
; int y
; int gen
; };
14 void add_point(struct entry
**board
, int x
, int y
, int gen
, int c
)
16 struct entry
*e
= calloc(1, sizeof(struct entry
));
17 e
->p
= (struct point
){.x
=x
, .y
=y
, .gen
=gen
};
19 HASH_ADD(hh
, *board
, p
, sizeof(struct point
), e
);
22 struct entry
*get_entry(struct entry
**board
, int x
, int y
, int gen
)
26 memset(&p
, 0, sizeof(p
));
30 HASH_FIND(hh
, *board
, &p
, sizeof(struct point
), e
);
34 void del_point(struct entry
**board
, int x
, int y
, int gen
)
36 struct entry
*e
= get_entry(board
, x
, y
, gen
);
41 int get_point(struct entry
**board
, int x
, int y
, int gen
)
43 struct entry
*e
= get_entry(board
, x
, y
, gen
);
45 return key
[0] == 1 ? gen
% 2 : 0;
49 char evolve(struct entry
**board
, int px
, int py
, int gen
)
52 for (int y
= py
-1; y
<=py
+1; y
++)
53 for (int x
= px
-1; x
<=px
+1; x
++)
54 r
= r
*2 + get_point(board
, x
, y
, gen
);
62 while ((c
= getchar()) != '\n')
63 key
[i
++] = c
== '#' ? 1 : 0;
64 if (getchar() != '\n')
67 struct entry
*board
= NULL
;
68 int x
= 0, minx
= 0, maxx
= 0;
69 int y
= 0, miny
= 0, maxy
= 0;
71 while ((c
= getchar()) != EOF
) {
78 add_point(&board
, x
++, y
, 0, c
== '#' ? 1 : 0);
84 for (int gen
= 1; gen
<= 50; gen
++) {
90 //Calculate r immediately all the time, saves 25ms
92 for (y
= miny
; y
<maxy
; y
++) {
93 for (x
= minx
; x
<maxx
; x
++) {
94 int nc
= evolve(&board
, x
, y
, gen
-1);
96 add_point(&board
, x
, y
, gen
, nc
);
99 //Delete old items (saves ±1second, 160k instead of 3.6 elems)
100 struct entry
*e
, *tmp
;
101 HASH_ITER(hh
, board
, e
, tmp
) {
102 if (e
->p
.gen
== gen
-1)