aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/01/part1.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
committerMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
commit6580dcd9127f69aaa794472ec92bc46015dc4019 (patch)
treedc2c7e102c75180f7bd98c2f3a14f8b55f83c0f2 /advent-of-code/2023/01/part1.c
init
Diffstat (limited to 'advent-of-code/2023/01/part1.c')
-rw-r--r--advent-of-code/2023/01/part1.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/advent-of-code/2023/01/part1.c b/advent-of-code/2023/01/part1.c
new file mode 100644
index 0000000..3aace2a
--- /dev/null
+++ b/advent-of-code/2023/01/part1.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+
+#include "str.h"
+
+
+int main() {
+ FILE* fp = fopen("./input", "r");
+ int sum = 0;
+ while (1) {
+ char *line = str_strip(fgetline(fp));
+ if (line == NULL || strlen(line) == 0) {
+ break;
+ }
+ int d1 = -1, d2 = -1;
+ while (*line != '\0') {
+ if (isdigit(*line)) {
+ if (d1 == -1) d1 = *line - '0';
+ d2 = *line - '0';
+ }
+ line++;
+ }
+ if (d2 == -1) d2 = d1;
+ sum += d1 * 10 + d2;
+ }
+ printf("%d\n", sum);
+ return 0;
+}
+