Let's replace a func call with a simple macro that just checks for if a character is between 0x30 ('0') to 0x39 ('9').

This commit is contained in:
Alexander Andreev 2023-12-03 18:36:14 +04:00
parent f4735ab54f
commit c2efe3c9f0
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 12 additions and 10 deletions

View File

@ -6,6 +6,8 @@
#include <stdbool.h>
#include <ctype.h>
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
int32_t parse_number(char *line, ssize_t j);
int main(int argc, char **argv) {
@ -48,38 +50,38 @@ int main(int argc, char **argv) {
int32_t n = 1;
int8_t p = 0;
if (isdigit(engine[i][j+1])) {
if (ISDIGIT(engine[i][j+1])) {
n *= parse_number(engine[i], j+1);
p++;
}
if (isdigit(engine[i][j-1])) {
if (ISDIGIT(engine[i][j-1])) {
n *= parse_number(engine[i], j-1);
p++;
}
if (isdigit(engine[i+1][j])) {
if (ISDIGIT(engine[i+1][j])) {
n *= parse_number(engine[i+1], j);
p++;
} else {
if (isdigit(engine[i+1][j+1])) {
if (ISDIGIT(engine[i+1][j+1])) {
n *= parse_number(engine[i+1], j+1);
p++;
}
if (isdigit(engine[i+1][j-1])) {
if (ISDIGIT(engine[i+1][j-1])) {
n *= parse_number(engine[i+1], j-1);
p++;
}
}
if (isdigit(engine[i-1][j])) {
if (ISDIGIT(engine[i-1][j])) {
n *= parse_number(engine[i-1], j);
p++;
} else {
if (isdigit(engine[i-1][j+1])) {
if (ISDIGIT(engine[i-1][j+1])) {
n *= parse_number(engine[i-1], j+1);
p++;
}
if (isdigit(engine[i-1][j-1])) {
if (ISDIGIT(engine[i-1][j-1])) {
n *= parse_number(engine[i-1], j-1);
p++;
}
@ -103,9 +105,9 @@ int32_t parse_number(char *line, ssize_t j) {
int32_t n = 0;
int32_t multiplier = 1;
while (isdigit(line[j+1])) j++;
while (ISDIGIT(line[j+1])) j++;
while (isdigit(line[j])) {
while (ISDIGIT(line[j])) {
n += (line[j]&0xf) * multiplier;
multiplier *= 10;
j--;