sudoku: add grid parsing functions

This commit is contained in:
Bruno BELANYI 2020-12-21 14:41:53 +01:00
parent 7129ce3a2b
commit 4b6bf824e6
3 changed files with 52 additions and 0 deletions

View file

@ -10,6 +10,7 @@ project(
sources = [ sources = [
'src/main.c', 'src/main.c',
'src/sudoku.c',
] ]
executable( executable(

37
src/sudoku.c Normal file
View file

@ -0,0 +1,37 @@
#include "sudoku.h"
#include <stdio.h>
#define INPUT_PATTERN "%1d %1d %1d %1d %1d %1d %1d %1d %1d\n"
bool parse_grid_file(struct sudoku *grid, FILE *input) {
if (!input || !grid)
return false;
for (size_t i = 0; i < 9; ++i) {
int *line = grid->grid[i];
if (fscanf(input, INPUT_PATTERN, &line[0], &line[1], &line[2], &line[3],
&line[4], &line[5], &line[6], &line[7], &line[8])
< 9)
return false;
}
return true;
}
bool parse_grid_str(struct sudoku *grid, const char *input) {
if (!input || !grid)
return false;
for (size_t i = 0; i < 9; ++i) {
int *line = grid->grid[i];
if (sscanf(input, INPUT_PATTERN, &line[0], &line[1], &line[2], &line[3],
&line[4], &line[5], &line[6], &line[7], &line[8])
< 9)
return false;
}
return true;
}

14
src/sudoku.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef SUDOKU_H
#define SUDOKU_H
#include <stdbool.h>
#include <stdio.h>
struct sudoku {
int grid[9][9];
};
bool parse_grid_file(struct sudoku *grid, FILE *input);
bool parse_grid_str(struct sudoku *grid, const char *input);
#endif /* !SUDOKU_H */