aboutsummaryrefslogtreecommitdiff
path: root/2023/01/1b.c
diff options
context:
space:
mode:
Diffstat (limited to '2023/01/1b.c')
-rw-r--r--2023/01/1b.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/2023/01/1b.c b/2023/01/1b.c
new file mode 100644
index 0000000..adc3da8
--- /dev/null
+++ b/2023/01/1b.c
@@ -0,0 +1,28 @@
1#include <stdio.h>
2#include <string.h>
3
4#define N 10000
5
6char *nums[10] = { "zero (unused)", "one", "two", "three", "four", "five",
7 "six", "seven", "eight", "nine" };
8
9int main() {
10 char *buf, line[N];
11 int j, first, last, sum;
12
13 sum = 0;
14 while ((buf = fgets(line, N, stdin)) != NULL) {
15 for (first = -1; *buf; buf++) {
16 for (j = 1; j < 10; j++)
17 if (!strncmp(buf, nums[j], strlen(nums[j])))
18 *buf = j + '0';
19 if (*buf < '0' || *buf > '9')
20 continue;
21 first = first == -1 ? *buf - '0' : first;
22 last = *buf - '0';
23 }
24 sum += 10 * first + last;
25 }
26 printf("%d\n", sum);
27 return 0;
28}

Generated with cgit - Back to sebastiano.tronto.net