aboutsummaryrefslogtreecommitdiff
path: root/2023/05
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-25 17:56:34 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-25 17:56:34 +0100
commit94d0033ed89bb5bfb6500051296892fb7cea6c29 (patch)
tree245eac2218310c958c2df2fad5109ea4f7b2a9d9 /2023/05
parent6a480c4eb9c96c82a8fd3663f0fbee4d32e56f19 (diff)
downloadaoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.tar.gz
aoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.zip
Small cleanup
Diffstat (limited to '2023/05')
-rw-r--r--2023/05/5a.c18
-rw-r--r--2023/05/5b.c10
2 files changed, 12 insertions, 16 deletions
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 @@
1#include <inttypes.h> 1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h> 2#include <stdio.h>
4#include <stdlib.h> 3#include <stdlib.h>
5#include <string.h> 4#include <string.h>
6 5
7#define N 100 6#define N 100
8 7#define ISNUM(c) (c >= '0' && c <= '9')
9bool isnum(char c) { return c >= '0' && c <= '9'; } 8#define MIN(x,y) ((x)<(y)?(x):(y))
10 9
11int main() { 10int main() {
12 char *buf, line[N]; 11 char *buf, line[N];
13 int64_t i, m, ns, seed[N], next[N], r[3]; 12 int64_t i, m, ns, seed[N], next[N], r[3];
14 13
15 for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) { 14 for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) {
16 if (!isnum(*buf)) continue; 15 if (!ISNUM(*buf)) continue;
17 next[ns++] = atoll(buf); 16 next[ns++] = atoll(buf);
18 while (isnum(*buf)) buf++; 17 while (ISNUM(*buf)) buf++;
19 } 18 }
20 19
21 20
22 while ((buf = fgets(line, N, stdin)) != NULL) { 21 while ((buf = fgets(line, N, stdin)) != NULL) {
23 if (!isnum(*buf)) { 22 if (!ISNUM(*buf)) {
24 memcpy(seed, next, ns * sizeof(int64_t)); 23 memcpy(seed, next, ns * sizeof(int64_t));
25 fgets(line, N, stdin); /* Discard description */ 24 fgets(line, N, stdin); /* Discard description */
26 continue; 25 continue;
@@ -28,7 +27,7 @@ int main() {
28 27
29 for (i = 0; *buf; buf++) { 28 for (i = 0; *buf; buf++) {
30 r[i++] = atoll(buf); 29 r[i++] = atoll(buf);
31 while (isnum(*buf)) buf++; 30 while (ISNUM(*buf)) buf++;
32 } 31 }
33 32
34 for (i = 0; i < ns; i++) 33 for (i = 0; i < ns; i++)
@@ -36,9 +35,8 @@ int main() {
36 next[i] = seed[i] + (r[0] - r[1]); 35 next[i] = seed[i] + (r[0] - r[1]);
37 } 36 }
38 37
39 m = next[0]; 38 for (i = 1, m = next[0]; i < ns; i++)
40 for (i = 1; i < ns; i++) 39 m = MIN(m, next[i]);
41 m = m > next[i] ? next[i] : m;
42 40
43 printf("%" PRId64 "\n", m); 41 printf("%" PRId64 "\n", m);
44 return 0; 42 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 @@
1/* For part 2 we save ranges as (first, last) instead of (first, length) */ 1/* For part 2 we save ranges as (first, last) instead of (first, length) */
2 2
3#include <inttypes.h> 3#include <inttypes.h>
4#include <stdbool.h>
5#include <stdio.h> 4#include <stdio.h>
6#include <stdlib.h> 5#include <stdlib.h>
7#include <string.h> 6#include <string.h>
8 7
9#define N 10000 8#define N 10000
10 9#define ISNUM(c) (c >= '0' && c <= '9')
11#define MIN(a, b) ((a)<(b)?(a):(b)) 10#define MIN(a, b) ((a)<(b)?(a):(b))
12#define MAX(a, b) ((a)>(b)?(a):(b)) 11#define MAX(a, b) ((a)>(b)?(a):(b))
13 12
14bool isnum(char c) { return c >= '0' && c <= '9'; }
15 13
16void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { 14void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) {
17 for (int i = 0; i < *nnr; i++) { 15 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) {
25int64_t readl(int64_t nums[], char *buf) { 23int64_t readl(int64_t nums[], char *buf) {
26 int64_t i; 24 int64_t i;
27 for (i = 0; *buf; buf++) { 25 for (i = 0; *buf; buf++) {
28 if (!isnum(*buf)) continue; 26 if (!ISNUM(*buf)) continue;
29 nums[i++] = atoll(buf); 27 nums[i++] = atoll(buf);
30 while (isnum(*buf)) buf++; 28 while (ISNUM(*buf)) buf++;
31 } 29 }
32 return i; 30 return i;
33} 31}
@@ -43,7 +41,7 @@ int main() {
43 } 41 }
44 42
45 for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) { 43 for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) {
46 if (!isnum(*buf)) { 44 if (!ISNUM(*buf)) {
47 append(range, next, &nr, &nnr); 45 append(range, next, &nr, &nnr);
48 continue; 46 continue;
49 } 47 }

Generated with cgit - Back to sebastiano.tronto.net