From 09f56fca17ffd61be30688181325b6485a0a2d7c Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Wed, 1 Dec 2021 09:43:34 -0500 Subject: [PATCH] day1: Add C solution --- .gitignore | 3 +++ 1/c/Makefile | 5 +++++ 1/c/a.c | 16 ++++++++++++++++ 1/c/b.c | 26 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 1/c/Makefile create mode 100644 1/c/a.c create mode 100644 1/c/b.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0b311e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# compiled solutions +a +b diff --git a/1/c/Makefile b/1/c/Makefile new file mode 100644 index 0000000..a2a6c98 --- /dev/null +++ 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 new file mode 100644 index 0000000..39ce157 --- /dev/null +++ b/1/c/a.c @@ -0,0 +1,16 @@ +#include +#include + +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 new file mode 100644 index 0000000..00e35c7 --- /dev/null +++ b/1/c/b.c @@ -0,0 +1,26 @@ +#include +#include + +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; +} -- libgit2 1.8.1