aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2022/03/2.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-01-27 14:28:51 +0800
committerMistivia <i@mistivia.com>2024-01-27 14:28:51 +0800
commit087a111b3417cbda03a3453b3d16dd4d1cf54a9e (patch)
tree000a15054865c3fb974970238568bb1d81a3f29e /advent-of-code/2022/03/2.c
parent203658f4a5b8649d0142ab8ff6440eb0eefa48e9 (diff)
add aoc 2022
Diffstat (limited to 'advent-of-code/2022/03/2.c')
-rw-r--r--advent-of-code/2022/03/2.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/advent-of-code/2022/03/2.c b/advent-of-code/2022/03/2.c
new file mode 100644
index 0000000..f91ea46
--- /dev/null
+++ b/advent-of-code/2022/03/2.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ + 1] = {0};
+
+int table[53] = {0};
+
+int decode(char c) {
+ if (c >= 'A' && c <= 'Z') {
+ return 26 + c - 'A' + 1;
+ }
+ if (c >='a' && c <= 'z') {
+ return c - 'a' + 1;
+ }
+ return -1;
+}
+
+int process(char *buf, int len, int n) {
+ if (n == 1) memset(table, 0, 53 * sizeof(int));
+ for (int i = 0; i < len; i++) {
+ int code = decode(buf[i]);
+ if (table[code] == n - 1) {
+ if (n == 3) return code;
+ table[code] = n;
+ }
+ }
+ return 0;
+}
+
+
+int main() {
+ FILE *fp = fopen("input", "r");
+ int sum = 0;
+ if (fp == NULL) return -1;
+ int line = 0;
+ while (fgets(buf, BUFSZ, fp)) {
+ if (strlen(buf) <= 1) continue;
+ int len = strlen(buf) - 1;
+ sum += process(buf, len, line % 3 + 1);
+ line++;
+ }
+ printf("%d\n", sum);
+}