From 94d0033ed89bb5bfb6500051296892fb7cea6c29 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 25 Dec 2023 17:56:34 +0100 Subject: Small cleanup --- 2023/05/5a.c | 18 ++++++++---------- 2023/05/5b.c | 10 ++++------ 2 files changed, 12 insertions(+), 16 deletions(-) (limited to '2023/05') diff --git a/2023/05/5a.c b/2023/05/5a.c index 15d2805..14b8c7c 100644 --- a/2023/05/5a.c +++ b/2023/05/5a.c @@ -1,26 +1,25 @@ #include -#include #include #include #include #define N 100 - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') +#define MIN(x,y) ((x)<(y)?(x):(y)) int main() { char *buf, line[N]; int64_t i, m, ns, seed[N], next[N], r[3]; for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) { - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; next[ns++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } while ((buf = fgets(line, N, stdin)) != NULL) { - if (!isnum(*buf)) { + if (!ISNUM(*buf)) { memcpy(seed, next, ns * sizeof(int64_t)); fgets(line, N, stdin); /* Discard description */ continue; @@ -28,7 +27,7 @@ int main() { for (i = 0; *buf; buf++) { r[i++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } for (i = 0; i < ns; i++) @@ -36,9 +35,8 @@ int main() { next[i] = seed[i] + (r[0] - r[1]); } - m = next[0]; - for (i = 1; i < ns; i++) - m = m > next[i] ? next[i] : m; + for (i = 1, m = next[0]; i < ns; i++) + m = MIN(m, next[i]); printf("%" PRId64 "\n", m); return 0; diff --git a/2023/05/5b.c b/2023/05/5b.c index aeac8c0..c59f61b 100644 --- a/2023/05/5b.c +++ b/2023/05/5b.c @@ -1,17 +1,15 @@ /* For part 2 we save ranges as (first, last) instead of (first, length) */ #include -#include #include #include #include #define N 10000 - +#define ISNUM(c) (c >= '0' && c <= '9') #define MIN(a, b) ((a)<(b)?(a):(b)) #define MAX(a, b) ((a)>(b)?(a):(b)) -bool isnum(char c) { return c >= '0' && c <= '9'; } void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { for (int i = 0; i < *nnr; i++) { @@ -25,9 +23,9 @@ void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { int64_t readl(int64_t nums[], char *buf) { int64_t i; for (i = 0; *buf; buf++) { - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; nums[i++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } return i; } @@ -43,7 +41,7 @@ int main() { } for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) { - if (!isnum(*buf)) { + if (!ISNUM(*buf)) { append(range, next, &nr, &nnr); continue; } -- cgit v1.3