advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit 51c0568fca5e7912677c7c770332129e643c0230
parent c2f92267749224cda5d4854ba1a4820306753202
Author: bsandro <[email protected]>
Date:   Sat, 11 Dec 2021 11:49:19 +0200

Day 11, puzzle 2

Diffstat:
Mday11/puzzle.c | 18++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/day11/puzzle.c b/day11/puzzle.c @@ -7,6 +7,7 @@ #include <strings.h> #include <assert.h> #include <ctype.h> +#include <limits.h> #include "util.h" @@ -48,15 +49,20 @@ void puzzle(const char *filename, long long *result1, long long *result2) { bzero(buf, STR_LEN); } - int flashes = 0; - - for (int s = 1; s <= STEPS; ++s) { + for (int s = 1; s < INT_MAX; ++s) { step(&octopuses); - flashes += normalize(&octopuses); + int flashes = normalize(&octopuses); + if (s <= STEPS) { + *result1 += flashes; + } + if (flashes == GRID_WIDTH*GRID_HEIGHT) { + *result2 = s; + } + if (*result1 > 0 && *result2 > 0) { + break; + } } - *result1 = flashes; - // mutiny! ignoring feof/ferror. fclose(infile); }