summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2020/AOC-1/main.c
blob: 070642fdaac9660b8e4a8e65c55f2a09588fb795 (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>
#include <assert.h>

#if 0
  #define PFILE "sample.txt"
  #define A_CAP 6
#else
  #define PFILE "input.txt"
  #define A_CAP 200
#endif

int ints[A_CAP];

void PrintInts()
{
  for(int i=0; i<A_CAP; i++)
    printf("%d\n", ints[i]);
}

void Parse()
{
  // Parse
  char ch;
  FILE *fp;
  fp = fopen(PFILE, "r");

  int i = 0;
  int n = 0;
  char *int_str = malloc(sizeof(char)*5);
  while((ch = fgetc(fp)) != EOF)
  {
    if(ch == '\n')
    {
      ints[i] = atoi(int_str);
      i += 1;
      n = 0;
      memset(int_str, 0, sizeof(char)*5);
      continue;
    }

    int_str[n] = ch;
    n += 1;
  }

  assert(i == A_CAP);

  free(int_str);
  fclose(fp);
}

void Part1()
{
  Parse();
  
  // Check
  for(int i=1; i<A_CAP; i++)
    for(int j=i; j<A_CAP; j++)
      if((ints[i-1] + ints[j]) == 2020)
	printf("RESULT-1: %llu\n", ints[i-1] * ints[j]);
}

void Part2()
{
  Parse();

  // Check
  for(int i=2; i<A_CAP; i++)
    for(int j=i; j<A_CAP; j++)
      for(int k=j; k<A_CAP; k++)
	if((ints[i-2] + ints[j-1] + ints[k]) == 2020)
	  printf("RESULT-2: %llu\n", ints[i-2] * ints[j-1] * ints[k]);
}

int main(void)
{
  Part1();
  Part2();
  return 0;
}