Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
49
2020/day01/day01.c
Normal file
49
2020/day01/day01.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <input file>\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(argv[1], "r");
|
||||
if (fp == NULL) {
|
||||
printf("Could not open file %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int input[1024] = {0};
|
||||
int d;
|
||||
int n = 0;
|
||||
while (fscanf(fp, "%d\n", &d) == 1) {
|
||||
input[n] = d;
|
||||
n++;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
int res = -1;
|
||||
for (int i = 0; i < n && res == -1; ++i) {
|
||||
for (int j = 0; j < n && res == -1; ++j) {
|
||||
if (input[i] + input[j] == 2020) {
|
||||
res = input[i] * input[j];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", res);
|
||||
|
||||
res = -1;
|
||||
for (int i = 0; i < n && res == -1; ++i) {
|
||||
for (int j = 0; j < n && res == -1; ++j) {
|
||||
for (int k = 0; k < n && res == -1; ++k) {
|
||||
if (input[i] + input[j] + input[k] == 2020) {
|
||||
res = input[i] * input[j] * input[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", res);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue