From c2efe3c9f0d42279233d634dfe06dff66d36d422 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 3 Dec 2023 18:36:14 +0400 Subject: [PATCH] Let's replace a func call with a simple macro that just checks for if a character is between 0x30 ('0') to 0x39 ('9'). --- day3/p2.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/day3/p2.c b/day3/p2.c index 753be5c..be9cf63 100644 --- a/day3/p2.c +++ b/day3/p2.c @@ -6,6 +6,8 @@ #include #include +#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--;