commit 09f56fca17ffd61be30688181325b6485a0a2d7c (patch)
parent 60bc03212b1eb9e94242074509b018881497c15d
Author: Alex Karle <alex@alexkarle.com>
Date: Wed, 1 Dec 2021 09:43:34 -0500
day1: Add C solution
Diffstat:
4 files changed, 50 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+# compiled solutions
+a
+b
diff --git a/1/c/Makefile b/1/c/Makefile
@@ -0,0 +1,5 @@
+a: a.c
+ $(CC) -o $@ $<
+
+b: b.c
+ $(CC) -o $@ $<
diff --git a/1/c/a.c b/1/c/a.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int main(void) {
+ int count = 0;
+ int curr;
+ int prev = 0;
+ while(fscanf(stdin, "%d\n", &curr) != EOF) {
+ if (prev && curr > prev) {
+ count++;
+ }
+ prev = curr;
+ }
+ printf("%d\n", count);
+ return 0;
+}
diff --git a/1/c/b.c b/1/c/b.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int mem[] = {0, 0, 0};
+int sum() {
+ return mem[0] + mem[1] + mem[2];
+}
+
+int main(void) {
+ int count = 0;
+ int curr;
+ int prev = 0;
+ int i = 0;
+ while(fscanf(stdin, "%d\n", &curr) != EOF) {
+ mem[i++ % 3] = curr;
+ if (i > 3) {
+ if (prev && sum() > prev) {
+ count++;
+ }
+ prev = sum();
+ }
+ i++;
+ }
+ printf("%d\n", count);
+ return 0;
+}