day20: final version (more comments, more code cleanup)
This commit is contained in:
@@ -241,3 +241,15 @@ aoc-c : res=318
|
|||||||
aoc-c : res=12166
|
aoc-c : res=12166
|
||||||
time: 0:00.06 real, 0.06 user, 0.00 sys
|
time: 0:00.06 real, 0.06 user, 0.00 sys
|
||||||
context-switch: 0+1, page-faults: 0+187
|
context-switch: 0+1, page-faults: 0+187
|
||||||
|
|
||||||
|
=========================================
|
||||||
|
================= day20 =================
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
aoc-c : res=5475
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+95
|
||||||
|
|
||||||
|
aoc-c : res=17548
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 1+1, page-faults: 0+108
|
||||||
|
@@ -21,15 +21,14 @@
|
|||||||
#include "bits.h"
|
#include "bits.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
#define ALGO 512
|
#define ALGOLEN 512
|
||||||
int algolen;
|
static char algorithm[ALGOLEN]; /* input algorithm */
|
||||||
char algo[ALGO + 1];
|
|
||||||
|
|
||||||
int universe_size = 0;
|
static int universe_size = 0; /* universe width/height */
|
||||||
char **universe[2];
|
static char **universe[2];
|
||||||
|
|
||||||
int left, right;
|
static int left, right; /* universe current borders */
|
||||||
int current = 0, next = 1;
|
static int current = 0; /* active universe */
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
/* print current universe
|
/* print current universe
|
||||||
@@ -50,7 +49,7 @@ static void print_data(int step)
|
|||||||
|
|
||||||
/* make one step
|
/* make one step
|
||||||
*/
|
*/
|
||||||
static int count()
|
static inline int count()
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
@@ -62,13 +61,14 @@ static int count()
|
|||||||
|
|
||||||
/* make one step
|
/* make one step
|
||||||
*/
|
*/
|
||||||
static void step()
|
static inline void step()
|
||||||
{
|
{
|
||||||
int newleft = left - 1, newright = right + 1;
|
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
for (int j = newleft; j < newright; ++j) {
|
left--;
|
||||||
for (int i = newleft; i < newright; ++i) {
|
right++;
|
||||||
|
for (int j = left; j < right; ++j) {
|
||||||
|
for (int i = left; i < right; ++i) {
|
||||||
index = universe[current][i-1][j-1] << 8 |
|
index = universe[current][i-1][j-1] << 8 |
|
||||||
universe[current][i-1][j ] << 7 |
|
universe[current][i-1][j ] << 7 |
|
||||||
universe[current][i-1][j+1] << 6 |
|
universe[current][i-1][j+1] << 6 |
|
||||||
@@ -80,74 +80,68 @@ static void step()
|
|||||||
universe[current][i+1][j-1] << 2 |
|
universe[current][i+1][j-1] << 2 |
|
||||||
universe[current][i+1][j ] << 1 |
|
universe[current][i+1][j ] << 1 |
|
||||||
universe[current][i+1][j+1] << 0;
|
universe[current][i+1][j+1] << 0;
|
||||||
universe[next][i][j] = algo[index];
|
universe[!current][i][j] = algorithm[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
left = newleft;
|
|
||||||
right = newright;
|
|
||||||
current = !current;
|
current = !current;
|
||||||
next = !next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read input
|
/* read input
|
||||||
*/
|
*/
|
||||||
static int read_data(int steps)
|
static int read_data(int steps)
|
||||||
{
|
{
|
||||||
int i = 0, cur = 0, verif;
|
int i = 0, cur = 0;
|
||||||
//char *buf, *buf1;
|
|
||||||
size_t alloc = 0;
|
size_t alloc = 0;
|
||||||
ssize_t buflen;
|
ssize_t buflen;
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
|
|
||||||
buflen = getline(&buf, &alloc, stdin) - 1;
|
buflen = getline(&buf, &alloc, stdin) - 1;
|
||||||
printf("len=%d\n", algolen);
|
|
||||||
algolen = buflen;
|
|
||||||
for (int i = 0; i < buflen; ++i)
|
for (int i = 0; i < buflen; ++i)
|
||||||
algo[i] = buf[i] == '#';
|
algorithm[i] = buf[i] == '#';
|
||||||
|
|
||||||
getline(&buf, &alloc, stdin);
|
|
||||||
|
|
||||||
/* We consider input will be a square, first line determines
|
/* We consider input will be a square, first line determines
|
||||||
* the width
|
* the width
|
||||||
*/
|
*/
|
||||||
|
getline(&buf, &alloc, stdin); /* skip second line */
|
||||||
buflen = getline(&buf, &alloc, stdin) - 1;
|
buflen = getline(&buf, &alloc, stdin) - 1;
|
||||||
verif = buflen;
|
|
||||||
|
/* universe grows by 1 on 4 side (= +2 width and +2 height),
|
||||||
|
* and we need also to keep one extra border for calculation.
|
||||||
|
*/
|
||||||
universe_size = buflen + 2 * steps + 2;
|
universe_size = buflen + 2 * steps + 2;
|
||||||
left = steps + 1;
|
left = steps + 1;
|
||||||
right = universe_size - left;
|
right = universe_size - left;
|
||||||
log_f(1, "buflen=%ld universe_size=%d left=%d\n",
|
|
||||||
buflen, universe_size, left);
|
|
||||||
universe[0] = malloc(universe_size * sizeof(char *));
|
universe[0] = malloc(universe_size * sizeof(char *));
|
||||||
universe[1] = malloc(universe_size * sizeof(char *));
|
universe[1] = malloc(universe_size * sizeof(char *));
|
||||||
for (i = 0; i < universe_size; ++i) {
|
for (i = 0; i < universe_size; ++i) {
|
||||||
universe[0][i] = calloc(universe_size, sizeof(char));
|
universe[0][i] = calloc(universe_size, sizeof(char));
|
||||||
universe[1][i] = calloc(universe_size, sizeof(char));
|
universe[1][i] = calloc(universe_size, sizeof(char));
|
||||||
}
|
}
|
||||||
/* they got me on this one ;-)
|
|
||||||
|
/* they got me on this one, different from given example ;-)
|
||||||
|
* We need to fill the odd universe with '1' if algorithm[0] is 1
|
||||||
|
* (whole universe becomes lit).
|
||||||
|
* It would be better to consider also the case algorithm[511] is 1,
|
||||||
|
* and algorithm[0] is 0 (we could only count after odd steps).
|
||||||
*/
|
*/
|
||||||
if (*algo) {
|
if (*algorithm) {
|
||||||
log(1, "init second array\n");
|
|
||||||
for (i = 0; i < universe_size; ++i)
|
for (i = 0; i < universe_size; ++i)
|
||||||
memset(universe[1][i], 1, universe_size);
|
memset(universe[1][i], 1, universe_size);
|
||||||
current=1;
|
if (algorithm[ALGOLEN - 1]) {
|
||||||
current=0;
|
log(0, "cannot display infinity ;-)\n");
|
||||||
}
|
return 0;
|
||||||
do {
|
|
||||||
for (int i = 0; i < buflen; ++i) {
|
|
||||||
universe[0][cur + left][i + left] = buf[i] == '#';
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
for (int i = 0; i < buflen; ++i)
|
||||||
|
universe[0][cur + left][i + left] = buf[i] == '#';
|
||||||
cur++;
|
cur++;
|
||||||
buflen = getline(&buf, &alloc, stdin) - 1;
|
buflen = getline(&buf, &alloc, stdin) - 1;
|
||||||
if (buflen != verif) {
|
} while (cur < universe_size && buflen > 0);
|
||||||
log(1, "verif: cur=%d buflen=%ld\n", cur, buflen);
|
|
||||||
}
|
|
||||||
} while (cur < universe_size && buflen == verif);
|
|
||||||
|
|
||||||
log_f(1, "cur=%d buflen=%ld universe_size=%d\n",
|
|
||||||
cur, buflen, universe_size);
|
|
||||||
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return algolen - 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int doit(int iter)
|
static int doit(int iter)
|
||||||
@@ -165,7 +159,7 @@ static int usage(char *prg)
|
|||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
int opt, part = 1, iter;
|
int opt, part = 1;
|
||||||
|
|
||||||
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
@@ -184,10 +178,11 @@ int main(int ac, char **av)
|
|||||||
if (optind < ac)
|
if (optind < ac)
|
||||||
return usage(*av);
|
return usage(*av);
|
||||||
|
|
||||||
iter = part == 1? 2: 50;
|
int iter = part == 1? 2: 50;
|
||||||
read_data(iter);
|
|
||||||
|
if (read_data(iter))
|
||||||
|
printf("%s : res=%d\n", *av, doit(iter));
|
||||||
|
|
||||||
printf("%s : res=%d\n", *av, doit(iter));
|
|
||||||
for (int i = 0; i < universe_size; ++i) {
|
for (int i = 0; i < universe_size; ++i) {
|
||||||
free(universe[0][i]);
|
free(universe[0][i]);
|
||||||
free(universe[1][i]);
|
free(universe[1][i]);
|
||||||
|
Reference in New Issue
Block a user