simplify day 11 (maybe too much)

This commit is contained in:
2021-12-12 20:31:24 +01:00
parent cede0eb93f
commit b06d784563

View File

@@ -22,16 +22,11 @@
#define MAX_GRID 10 #define MAX_GRID 10
static int grid[MAX_GRID][MAX_GRID]; static int grid[MAX_GRID][MAX_GRID];
static int gsize; /* grid size */ static int gsize; /* real grid size */
static int stack[MAX_GRID * MAX_GRID]; /* flashing stack */ static int stack[MAX_GRID * MAX_GRID]; /* flashing stack */
static int nstack; /* current stack size */ static int nstack; /* current stack size */
#define loop_for_grid(l, c, val) \
for (l = 1; l <= gsize; ++l) \
for (c = 1; c <= gsize; ++c) \
val = grid[l][c];
#define VALID(x, y) ((x) >= 0 && (y) >= 0 && (x) < gsize && (y) < gsize) #define VALID(x, y) ((x) >= 0 && (y) >= 0 && (x) < gsize && (y) < gsize)
inline static int push(int l, int c) inline static int push(int l, int c)
@@ -41,9 +36,7 @@ inline static int push(int l, int c)
inline static int pop() inline static int pop()
{ {
if (!nstack) return nstack? stack[--nstack]: -1;
return -1;
return stack[--nstack];
} }
#ifdef DEBUG #ifdef DEBUG
@@ -74,47 +67,39 @@ static int do_step()
int vpop, flash = 0; int vpop, flash = 0;
/* 1) increase all energy levels */ /* 1) increase all energy levels */
for (int l = 0; l < gsize; ++l) { for (int l = 0; l < gsize; ++l)
for (int c = 0; c < gsize; ++c) { for (int c = 0; c < gsize; ++c)
flash += flash_cell_maybe(l, c); flash += flash_cell_maybe(l, c);
}
}
/* 2) perform BFS until stack is empty */ /* 2) perform BFS until stack is empty */
while ((vpop = pop()) != -1) { while ((vpop = pop()) != -1)
int pl = vpop / gsize, pc = vpop % gsize; for (int l = -1, pl = vpop / gsize, pc = vpop % gsize; l <= 1; ++l)
for (int l = -1; l <= 1; ++l) { for (int c = -1; c <= 1; ++c)
for (int c = -1; c <= 1; ++c) {
flash += flash_cell_maybe(pl + l, pc + c); flash += flash_cell_maybe(pl + l, pc + c);
}
}
}
/* 3) cleanup flashed cells */ /* 3) cleanup flashed cells */
for (int l = 0; l < gsize; ++l) { for (int l = 0; l < gsize; ++l)
for (int c = 0; c < gsize; ++c) { for (int c = 0; c < gsize; ++c)
if (grid[l][c] > 9) if (grid[l][c] > 9)
grid[l][c] = 0; grid[l][c] = 0;
}
}
return flash; return flash;
} }
static int part1(int nsteps) static int part1()
{ {
int flash = 0; int flash = 0;
for (int step = 1; step <= nsteps; ++step) for (int step = 0; step < 100; ++step)
flash += do_step(); flash += do_step();
return flash; return flash;
} }
static int part2() static int part2()
{ {
int flash = 0, step; int step = 1;
for (step = 0; flash != 100; ++step) while (do_step() != 100)
flash = do_step(step); step++;
return step; return step;
} }
@@ -134,7 +119,7 @@ static int doit(int part)
getline(&buf, &alloc, stdin); getline(&buf, &alloc, stdin);
} }
free(buf); free(buf);
return part == 1? part1(100): part2(); return part == 1? part1(): part2();
} }
static int usage(char *prg) static int usage(char *prg)
@@ -145,12 +130,12 @@ static int usage(char *prg)
int main(int ac, char **av) int main(int ac, char **av)
{ {
int opt, res, part = 1, debug = 0; int opt, part = 1;
while ((opt = getopt(ac, av, "d:p:")) != -1) { while ((opt = getopt(ac, av, "d:p:")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
debug = atoi(optarg); debug_level_set(atoi(optarg));
break; break;
case 'p': /* 1 or 2 */ case 'p': /* 1 or 2 */
part = atoi(optarg); part = atoi(optarg);
@@ -161,11 +146,9 @@ int main(int ac, char **av)
return usage(*av); return usage(*av);
} }
} }
debug_level_set(debug);
if (optind < ac) if (optind < ac)
return usage(*av); return usage(*av);
res = doit(part); printf("%s : res=%d\n", *av, doit(part));
printf("%s : res=%d\n", *av, res);
exit (0); exit (0);
} }