summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-1/main.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-07-31 11:55:45 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-07-31 11:55:45 +0300
commit48966f12832ac97228132e56fb3159099f3e466e (patch)
tree9cc37aab6d00230787e19b68127abd2b818068ef /Advent-of-Code-2021/AOC-1/main.c
parentaec1c07260257ba7c28eff53f422ddb7daaf316a (diff)
cleanup
Diffstat (limited to 'Advent-of-Code-2021/AOC-1/main.c')
-rw-r--r--Advent-of-Code-2021/AOC-1/main.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/Advent-of-Code-2021/AOC-1/main.c b/Advent-of-Code-2021/AOC-1/main.c
new file mode 100644
index 0000000..5574481
--- /dev/null
+++ b/Advent-of-Code-2021/AOC-1/main.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define N 5000
+
+int numbers[N];
+
+void ParseInput(char *filepath)
+{
+ char ch;
+ FILE *fp;
+ fp = fopen(filepath, "r");
+
+ if (fp == NULL)
+ {
+ perror("Error while opening the file.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ int i = 0;
+ int n = 0;
+ char *str = malloc(sizeof(char)*N);
+ while((ch= fgetc(fp)) != EOF)
+ {
+ if(ch != '\n')
+ {
+ str[i] = ch;
+ i += 1;
+ }
+ else
+ {
+ i = 0;
+ numbers[n] = atoi(str);
+ n += 1;
+ memset(str, ' ', sizeof(char)*N);
+ }
+ }
+
+ free(str);
+
+ fclose(fp);
+}
+
+void PrintNumbers()
+{
+ for(int i=0; i<N; i++)
+ {
+ printf("%d\n", numbers[i]);
+ }
+}
+
+int main (void)
+{
+ ParseInput("input-1.txt");
+
+ /* Part One
+ int bigger = 0;
+ for(int i=1; i<N; i++)
+ {
+ if(numbers[i] > numbers[(i-1)])
+ bigger += 1;
+ }
+
+ printf("Answer: %d", bigger);
+ */
+
+ // Part Two
+ int bigger = 0;
+ for(int i = 3; i<N; i++)
+ {
+ if(
+ (numbers[i-2] + numbers[i-1] + numbers[i]) >
+ (numbers[i-3] + numbers[i-2] + numbers[i -1]))
+ bigger += 1;
+ }
+
+ printf("Answer: %d", bigger);
+
+ return 0;
+}