advent2021

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

commit f8010e86532cb3acfd44ca8aabe39d7eac411907
parent 0e7e16473058809784f0a6a04508da839423a744
Author: bsandro <brian.drosan@gmail.com>
Date:   Sat, 25 Dec 2021 13:38:28 +0200

Day 25 puzzle 1

Diffstat:
Mday25/puzzle.c | 92++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 51 insertions(+), 41 deletions(-)

diff --git a/day25/puzzle.c b/day25/puzzle.c @@ -25,8 +25,14 @@ struct tile_t { struct cucumber_t *cucumber; }; -void print_tiles(struct tile_t (*tiles)[9][10]); -int step(struct tile_t (*tiles)[9][10], uint64_t step); +struct map_t { + struct tile_t *tiles; + int width; + int height; +}; + +void print_map(struct map_t *map); +int step(struct map_t *map, uint64_t step); void puzzle(const char *filename, long long *result1, long long *result2) { FILE *infile = fopen(filename, "r"); @@ -37,13 +43,18 @@ void puzzle(const char *filename, long long *result1, long long *result2) { char buf[STR_LEN] = {0}; size_t line = 0; - - // test data only - struct tile_t tiles[9][10] = {0}; + struct map_t map = {0}; while (fgets(buf, STR_LEN, infile) != NULL) { int len = strlen(buf); assert(len > 0); + if (map.width > 0 && map.width != len-1) { + printf("invalid input\n"); + exit(1); + } + map.width = len-1; + line = map.height++; + map.tiles = realloc(map.tiles, sizeof(struct tile_t) * map.width * map.height); for (int i = 0; i < len-1; ++i) { char c = buf[i]; if (c == '>' || c == 'v') { @@ -52,38 +63,37 @@ void puzzle(const char *filename, long long *result1, long long *result2) { cucumber->direction = (c == '>') ? EAST : SOUTH; cucumber->stuck = false; cucumber->step = 0; - tiles[line][i].cucumber = cucumber; + map.tiles[line*map.width+i].cucumber = cucumber; + } else { + map.tiles[line*map.width+i].cucumber = NULL; } } - line++; bzero(buf, STR_LEN); } - //print_tiles(&tiles); + //printf("map width: %d, height: %d\n", map.width, map.height); + //print_map(&map); for (uint64_t i = 1;; ++i) { - //printf("- step %llu -\n", i); - if (step(&tiles, i) == 0) { - //printf("step %llu is final\n", i); - //print_tiles(&tiles); + if (step(&map, i) == 0) { *result1 = i; break; } - //print_tiles(&tiles); } - + //print_map(&map); *result2 = 0; // mutiny! ignoring feof/ferror. fclose(infile); } -void print_tiles(struct tile_t (*tiles)[9][10]) { +void print_map(struct map_t *map) { + assert(map != NULL); printf("----------\n"); - for (int y = 0; y < 9; ++y) { - for (int x = 0; x < 10; ++x) { - if ((*tiles)[y][x].cucumber != NULL) { - struct cucumber_t *cucumber = (*tiles)[y][x].cucumber; - printf("%c", cucumber->direction == SOUTH ? 'v' : '>'); + for (int y = 0; y < map->height; ++y) { + for (int x = 0; x < map->width; ++x) { + struct tile_t *tile = &map->tiles[y*map->width+x]; + if (tile->cucumber != NULL) { + printf("%c", tile->cucumber->direction == SOUTH ? 'v' : '>'); } else { printf("."); } @@ -93,24 +103,24 @@ void print_tiles(struct tile_t (*tiles)[9][10]) { printf("\n"); } -int step(struct tile_t (*tiles)[9][10], uint64_t step) { +int step(struct map_t *map, uint64_t step) { int moved = 0; // eastbound cucumbers - for (int y = 0; y < 9; ++y) { - for (int x = 0; x < 10; ++x) { - int nx = (x == 10-1) ? 0 : x+1; - struct tile_t *tile = &(*tiles)[y][x]; - struct tile_t *next = &(*tiles)[y][nx]; + for (int y = 0; y < map->height; ++y) { + for (int x = 0; x < map->width; ++x) { + int nx = (x == map->width-1) ? 0 : x+1; + struct tile_t *tile = &map->tiles[map->width*y+x]; + struct tile_t *next = &map->tiles[map->width*y+nx]; if (tile->cucumber != NULL && tile->cucumber->direction == EAST && tile->cucumber->step != step) { tile->cucumber->stuck = next->cucumber != NULL; } } } - for (int y = 0; y < 9; ++y) { - for (int x = 0; x < 10; ++x) { - int nx = (x == 10-1) ? 0 : x+1; - struct tile_t *tile = &(*tiles)[y][x]; - struct tile_t *next = &(*tiles)[y][nx]; + for (int y = 0; y < map->height; ++y) { + for (int x = 0; x < map->width; ++x) { + int nx = (x == map->width-1) ? 0 : x+1; + struct tile_t *tile = &map->tiles[map->width*y+x]; + struct tile_t *next = &map->tiles[map->width*y+nx]; if (tile->cucumber != NULL && tile->cucumber->direction == EAST && !tile->cucumber->stuck && tile->cucumber->step != step) { assert(next->cucumber == NULL); tile->cucumber->step = step; @@ -121,21 +131,21 @@ int step(struct tile_t (*tiles)[9][10], uint64_t step) { } } // southbound cucumbers - for (int y = 0; y < 9; ++y) { - int ny = (y == 9-1) ? 0 : y+1; - for (int x = 0; x < 10; ++x) { - struct tile_t *tile = &(*tiles)[y][x]; - struct tile_t *next = &(*tiles)[ny][x]; + for (int y = 0; y < map->height; ++y) { + int ny = (y == map->height-1) ? 0 : y+1; + for (int x = 0; x < map->width; ++x) { + struct tile_t *tile = &map->tiles[map->width*y+x]; + struct tile_t *next = &map->tiles[map->width*ny+x]; if (tile->cucumber != NULL && tile->cucumber->direction == SOUTH && tile->cucumber->step != step) { tile->cucumber->stuck = next->cucumber != NULL; } } } - for (int y = 0; y < 9; ++y) { - int ny = (y == 9-1) ? 0 : y+1; - for (int x = 0; x < 10; ++x) { - struct tile_t *tile = &(*tiles)[y][x]; - struct tile_t *next = &(*tiles)[ny][x]; + for (int y = 0; y < map->height; ++y) { + int ny = (y == map->height-1) ? 0 : y+1; + for (int x = 0; x < map->width; ++x) { + struct tile_t *tile = &map->tiles[map->width*y+x]; + struct tile_t *next = &map->tiles[map->width*ny+x]; if (tile->cucumber != NULL && tile->cucumber->direction == SOUTH && !tile->cucumber->stuck && tile->cucumber->step != step) { assert(next->cucumber == NULL); tile->cucumber->step = step;