From 1d9edb73c1a9bad0e34acadef30282251f273db5 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 16 Jul 2021 21:01:41 +0200 Subject: [PATCH] Day 17 part 1 (c) removed debug code. --- day17/OUTPUT | 16 +-- day17/ex1-c.c | 273 +------------------------------------------------- 2 files changed, 15 insertions(+), 274 deletions(-) diff --git a/day17/OUTPUT b/day17/OUTPUT index f2fbd3f..506d068 100644 --- a/day17/OUTPUT +++ b/day17/OUTPUT @@ -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 diff --git a/day17/ex1-c.c b/day17/ex1-c.c index 2ab5873..c5c3c9e 100644 --- a/day17/ex1-c.c +++ b/day17/ex1-c.c @@ -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; kstate != 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; xstate, 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; zset, 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; ilast; - unsigned ncols=plane->ncol; - struct seats *ptr=plane->seats; - - for (i=0; i0 && !(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; i0 && !(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; ineighbours=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= 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 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; -} -*/