summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-1/main.c
blob: 5574481f7a562f2342d2edec384e1d003ce0b3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
}