Update solution for 2021 day 09

- close the file handle
- #define max map x and y values
This commit is contained in:
Patrick Auernig 2021-12-09 21:24:53 +01:00
parent 2204d3a32f
commit 4b895a6c66
4 changed files with 9 additions and 5 deletions

View File

@ -1,5 +1,5 @@
@part PART INPUT_FILE="inputs/puzzle.txt": @part PART INPUT_FILE="inputs/puzzle.txt":
gcc -std=c17 -Werror -Wextra -o part_{{PART}} common.c part_{{PART}}.c gcc -std=c17 -Werror -Wextra -pedantic -o part_{{PART}} common.c part_{{PART}}.c
./part_{{PART}} {{INPUT_FILE}} ./part_{{PART}} {{INPUT_FILE}}
clean: clean:

View File

@ -6,7 +6,7 @@
int MAP_WIDTH = 0; int MAP_WIDTH = 0;
int MAP_HEIGHT = 0; int MAP_HEIGHT = 0;
int MAP[100][100]; int MAP[MAP_MAX_Y][MAP_MAX_X];
void read_file(char* path) { void read_file(char* path) {
FILE* fp = fopen(path, "r"); FILE* fp = fopen(path, "r");
@ -37,6 +37,8 @@ void read_file(char* path) {
} }
MAP_HEIGHT = y; MAP_HEIGHT = y;
fclose(fp);
} }
void print_map(int map[100][100]) { void print_map(int map[100][100]) {

View File

@ -1,8 +1,10 @@
#pragma once #pragma once
#define MAP_MAX_Y 100
#define MAP_MAX_X 100
extern int MAP_WIDTH; extern int MAP_WIDTH;
extern int MAP_HEIGHT; extern int MAP_HEIGHT;
extern int MAP[100][100]; extern int MAP[MAP_MAX_Y][MAP_MAX_X];
typedef int (*result_fn)(int, int, int); typedef int (*result_fn)(int, int, int);

View File

@ -7,7 +7,7 @@
int BASIN_IDX = 0; int BASIN_IDX = 0;
int BASINS[BASIN_COUNT] = { 0 }; int BASINS[BASIN_COUNT] = { 0 };
int scout_basin(int x, int y, int marked[100][100]) { int scout_basin(int x, int y, int marked[MAP_MAX_Y][MAP_MAX_X]) {
marked[y][x] = 1; marked[y][x] = 1;
int count = 1; int count = 1;
@ -31,7 +31,7 @@ int scout_basin(int x, int y, int marked[100][100]) {
} }
int calculate_basin_size(int x, int y, int cur) { int calculate_basin_size(int x, int y, int cur) {
int marked[100][100] = { 0 }; int marked[MAP_MAX_Y][MAP_MAX_X] = { 0 };
int basin_size = scout_basin(x, y, marked); int basin_size = scout_basin(x, y, marked);
BASINS[BASIN_IDX] = basin_size; BASINS[BASIN_IDX] = basin_size;
BASIN_IDX++; BASIN_IDX++;