Pass just a line to parse_number().

This commit is contained in:
Alexander Andreev 2023-12-03 18:31:34 +04:00
parent fabaaffe11
commit f4735ab54f
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -6,7 +6,7 @@
#include <stdbool.h>
#include <ctype.h>
int32_t parse_number(char **engine, ssize_t i, ssize_t j);
int32_t parse_number(char *line, ssize_t j);
int main(int argc, char **argv) {
FILE *input;
@ -49,38 +49,38 @@ int main(int argc, char **argv) {
int8_t p = 0;
if (isdigit(engine[i][j+1])) {
n *= parse_number(engine, i, j+1);
n *= parse_number(engine[i], j+1);
p++;
}
if (isdigit(engine[i][j-1])) {
n *= parse_number(engine, i, j-1);
n *= parse_number(engine[i], j-1);
p++;
}
if (isdigit(engine[i+1][j])) {
n *= parse_number(engine, i+1, j);
n *= parse_number(engine[i+1], j);
p++;
} else {
if (isdigit(engine[i+1][j+1])) {
n *= parse_number(engine, i+1, j+1);
n *= parse_number(engine[i+1], j+1);
p++;
}
if (isdigit(engine[i+1][j-1])) {
n *= parse_number(engine, i+1, j-1);
n *= parse_number(engine[i+1], j-1);
p++;
}
}
if (isdigit(engine[i-1][j])) {
n *= parse_number(engine, i-1, j);
n *= parse_number(engine[i-1], j);
p++;
} else {
if (isdigit(engine[i-1][j+1])) {
n *= parse_number(engine, i-1, j+1);
n *= parse_number(engine[i-1], j+1);
p++;
}
if (isdigit(engine[i-1][j-1])) {
n *= parse_number(engine, i-1, j-1);
n *= parse_number(engine[i-1], j-1);
p++;
}
}
@ -99,14 +99,14 @@ int main(int argc, char **argv) {
return 0;
}
int32_t parse_number(char **engine, ssize_t i, ssize_t j) {
int32_t parse_number(char *line, ssize_t j) {
int32_t n = 0;
int32_t multiplier = 1;
while (isdigit(engine[i][j+1])) j++;
while (isdigit(line[j+1])) j++;
while (isdigit(engine[i][j])) {
n += (engine[i][j]&0xf) * multiplier;
while (isdigit(line[j])) {
n += (line[j]&0xf) * multiplier;
multiplier *= 10;
j--;
}