Day 17 part 1 (c) removed debug code.

This commit is contained in:
2021-07-16 21:01:41 +02:00
parent 1386061fbc
commit 1d9edb73c1
2 changed files with 15 additions and 274 deletions

View File

@@ -1,11 +1,15 @@
ex1-v1.bash : res=263
time: 1:25.64 real, 61.31 user, 30.44 sys
context-switch: 5732+166356, page-faults: 0+7279275
time: 1:28.00 real, 62.50 user, 31.11 sys
context-switch: 5860+165603, page-faults: 0+7574999
ex1.bash : res=263
time: 0:00.40 real, 0.40 user, 0.00 sys
context-switch: 41+1, page-faults: 0+315
time: 0:00.37 real, 0.37 user, 0.00 sys
context-switch: 5+1, page-faults: 0+321
ex1-c : res=263
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+149
ex2.bash : res=1680
time: 0:07.88 real, 7.87 user, 0.00 sys
context-switch: 815+1, page-faults: 0+2637
time: 0:06.50 real, 6.43 user, 0.03 sys
context-switch: 52+14, page-faults: 0+2873

View File

@@ -31,8 +31,6 @@ LIST_HEAD(qviewed);
static CELL cube[SIZE][SIZE][SIZE];
#define R(x) ((x)+ROOT)
#define SET(pcell,x,y,z) (pcell[R(x)]=
#define CUBE2X(cell) (((cell)-&cube[0][0][0])/SIZE/SIZE)
#define CUBE2Y(cell) (((cell)-&cube[0][0][0])/SIZE%SIZE)
#define CUBE2Z(cell) (((cell)-&cube[0][0][0])/(SIZE/SIZE)%SIZE)
@@ -47,9 +45,8 @@ static void reset_cell(CELL *pcell)
pcell->oldstate=INACTIVE;
pcell->visited=false;
pcell->count=0;
//HEADRESET(&pcell->set);
//HEADRESET(&pcell->viewed);
}
static void reset_cube()
{
int i, j, k;
@@ -59,91 +56,6 @@ static void reset_cube()
for (k=0; k<SIZE; ++k)
reset_cell(&cube[i][j][k]);
}
static void check_cube_sanity()
{
CELL *pos;
int x, y, z, count1=0, count2=0, count=0;
/* check state of active cells */
list_for_each_entry(pos, &qset, set) {
count1++;
if (pos->state != ACTIVE)
printf("cell (%ld,%ld,%ld): state is not ACTIVE.\n",
CUBE2X(pos), CUBE2Y(pos), CUBE2Z(pos));
if (pos->visited != 1)
printf("cell (%ld,%ld,%ld): state is not VISITED.\n",
CUBE2X(pos), CUBE2Y(pos), CUBE2Z(pos));
}
for (x=0; x<SIZE; ++x) {
for (y=0; y<SIZE; ++y) {
for (z=0; z<SIZE; ++z) {
if (cube[x][y][z].state == ACTIVE) {
count2++;
if (cube[x][y][z].count != 0)
count++;
if (cube[x][y][z].visited != 1)
printf("cell (%d,%d,%d): ACTIVE & not VISITED.\n",
x, y, z);
} else {
if (cube[x][y][z].visited != 0)
printf("cell (%d,%d,%d): INACTIVE & VISITED.\n",
x, y, z);
}
}
}
}
printf("sanity check: count1=%d, count2=%d count=%d\n",
count1, count2, count);
}
static void print_cell(CELL *p)
{
printf("(%ld,%ld,%ld): st=%c vi=%d co=%d\n",
CUBE2X(p), CUBE2Y(p), CUBE2Z(p),
p->state, p->visited, p->count);
}
static void set_print_cell(struct list_head *p)
{
print_cell(list_entry(p, CELL, set));
}
static void viewed_print_cell(struct list_head *p)
{
print_cell(list_entry(p, CELL, viewed));
}
static void print_cube()
{
int x, y, z;
for (z=0; z<SIZE; ++z) { /* # plan */
printf("z=%d\n", z-ZERO);
for (y=0; y<SIZE; ++y) {
for (x=0; x<SIZE; ++x) {
putchar(cube[x][y][z].state);
}
putchar('\n');
}
putchar('\n');
}
}
void print_set()
{
struct cell *pos;
printf("set: %p n=%p p=%p\n", &qset, qset.next, qset.prev);
list_for_each_entry(pos, &qset, set) {
printf(" %p: %p %p - %ld %ld %ld\n",
&pos->set, pos->set.prev, pos->set.next,
CUBE2X(pos), CUBE2Y(pos), CUBE2Z(pos));
}
}
void print_viewed()
{
struct cell *pos;
printf("viewed: %p n=%p p=%p\n", &qviewed, qviewed.next, qviewed.prev);
list_for_each_entry(pos, &qviewed, viewed) {
printf(" %p: %p %p\n", &pos->viewed, pos->viewed.prev, pos->viewed.next);
}
}
static int count_active()
{
@@ -155,14 +67,13 @@ static int count_active()
}
return i;
}
/* fill initial plane */
static void add_row(char *line, int row)
static void init_row(char *line, int row)
{
int i, x, y, z;
static int ncols=0;
CELL *pcell;
printf("LINE %d: %lu %s\n", row, strlen(line)-1, line);
if (ncols == 0)
ncols=strlen(line)-1;
y=ZERO-ncols/2+row;
@@ -171,15 +82,11 @@ static void add_row(char *line, int row)
for (i=0; *line; ++i, ++line) {
x=ZERO+i-ncols/2;
pcell=&cube[x][y][z];
printf("pos=(%d,%d,%d): %#x ", x, y, z, *line);
printf(" --> (%ld, %ld, %ld)\n", CUBE2X(pcell),
CUBE2Y(pcell), CUBE2Z(pcell));
if (*line == '#') {
list_add(&pcell->set, &qset);
list_add(&pcell->viewed, &qviewed);
pcell->state=ACTIVE;
pcell->visited=1;
//print_set();
}
}
}
@@ -209,9 +116,6 @@ void run_life()
}
}
}
/*print_set();
print_viewed();*/
/* 2) apply rules for all visited cells */
list_for_each_entry_safe(pcell, ptmp, &qviewed, viewed) {
switch (pcell->state) {
case ACTIVE:
@@ -235,15 +139,6 @@ void run_life()
list_del(&pcell->viewed);
}
}
printf("active cells:\n");
list_for_each_entry(pcell, &qset, set) {
print_cell(pcell);
}
printf("viewed cells:\n");
list_for_each_entry(pcell, &qviewed, viewed) {
print_cell(pcell);
}
check_cube_sanity();
}
int main(ac, av)
@@ -256,173 +151,15 @@ int main(ac, av)
reset_cube();
INIT_LIST_HEAD(&qset);
INIT_LIST_HEAD(&qviewed);
printf("set: %p n=%p p=%p\n", &qset, qset.next, qset.prev);
printf("vis: %p n=%p p=%p\n", &qviewed, qviewed.next, qviewed.prev);
while (fgets(line, sizeof line, stdin)) {
add_row(line, nline);
init_row(line, nline);
nline++;
}
for (int i=0; i<LOOPS; ++i)
run_life();
/*print_cube();
run_life();
print_cube();
run_life();
print_cube();
run_life();
print_cube();*/
printf("%s : res=%d\n", *av, count_active());
exit (0);
}
/*
void print_count(plane)
struct plane *plane;
{
unsigned i, psize=plane->last;
unsigned ncols=plane->ncol;
struct seats *ptr=plane->seats;
for (i=0; i<psize; ++i) {
if (i>0 && !(i%ncols)) {
putchar('\n');
}
printf("%2d ", (ptr+i)->neighbours);
}
putchar('\n');
}
void print_seats(plane)
struct plane *plane;
{
unsigned i, psize=plane->last;
unsigned ncols=plane->ncol, nrow=plane->nrow;
struct seats *ptr=plane->seats;
fprintf(stderr, "PLANE: address=%p seat=%p rows=%d cols=%d size=%d\n",
plane, ptr, nrow, ncols, psize);
for (i=0; i<psize; ++i) {
if (i>0 && !(i%ncols)) {
putchar('\n');
}
printf("%c ", (ptr+i)->status);
}
putchar('\n');
}
void reset_seats(plane)
struct plane *plane;
{
unsigned i, last=plane->last;
struct seats *seat=plane->seats;
for (i=0; i<last; ++i, ++seat)
seat->neighbours=0;
}
struct plane *add_row(plane, c)
struct plane *plane;
char *c;
{
unsigned size;
if (!plane) {
plane=malloc(sizeof(struct plane));
plane->seats=malloc(sizeof(struct seats)*BLOCKSIZE);
plane->size=BLOCKSIZE;
plane->ncol=strlen(c)-1;
plane->last=0;
}
size=plane->size;
while (*c) {
if (*c != '\n') {
if (plane->last == size) {
size+=BLOCKSIZE;
plane->size=size;
plane->seats=realloc(plane->seats, sizeof(struct seats)*size);
}
plane->seats[plane->last].status=*c;
plane->last++;
plane->nrow=plane->last/plane->ncol;
}
c++;
}
//sscanf(line, "%d", &val);
return plane;
}
int sit(plane)
struct plane *plane;
{
unsigned changed=0, cur, seated=0;
unsigned last=plane->last;
struct seats *seats=plane->seats;
for (cur=0; cur<last; ++cur) {
switch (seats[cur].status) {
case '.':
break;
case '#':
if (seats[cur].neighbours >= 4) {
seats[cur].status='L';
changed++;
} else {
seated++;
}
break;
case 'L':
if (seats[cur].neighbours == 0) {
seats[cur].status='#';
changed++;
seated++;
}
break;
}
}
plane->seated=seated;
return changed;
}
int calc(plane)
struct plane *plane;
{
unsigned row, col, cur;
unsigned last=plane->last;
unsigned cols=plane->ncol;
unsigned rows=plane->nrow;
struct seats *seats=plane->seats;
for (cur=0; cur<last; ++cur) {
row=cur/cols;
col=cur%cols;
if (seats[cur].status == '#') {
if (row > 0) {
seats[cur - cols].neighbours++;
if (col > 0)
seats[cur - cols - 1].neighbours++;
if (col < (cols-1))
seats[cur - cols + 1].neighbours++;
}
if (col > 0) {
seats[cur - 1].neighbours++;
if (row < (rows-1))
seats[cur + cols - 1].neighbours++;
}
if (col < (cols-1)) {
seats[cur + 1].neighbours++;
if (row < (rows-1))
seats[cur + cols + 1].neighbours++;
}
if (row < (rows-1))
seats[cur + cols].neighbours++;
}
}
return 1;
}
*/