From 138b75f536df595b8f5e12a7dced42bed2a165f6 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 11 Dec 2023 15:17:51 -0500 Subject: Solve day 3 part 1 --- 2023/day03.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2023/day03.c diff --git a/2023/day03.c b/2023/day03.c new file mode 100644 index 0000000..eba3382 --- /dev/null +++ b/2023/day03.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +int main(void) { + int sum = 0; + char g[150][150]; + int h = 0; + while (gets(g[h++])); + int w = strlen(g[--h-1]); + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) { + if (g[y][x] == '.' || isdigit(g[y][x])) continue; + for (int i = -1; i <= +1; ++i) + for (int j = -1; j <= +1; ++j) { + char *p = &g[y+i][x+j]; + if (!isdigit(*p)) continue; + while (p > g[y+i] && isdigit(p[-1])) p--; + sum += atoi(p); + while (isdigit(*p)) *p++ = '.'; + } + } + printf("%d\n", sum); +} -- cgit 1.4.1