diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-10-29 23:02:39 +0200 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-10-29 23:02:39 +0200 |
commit | e7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch) | |
tree | 237261eef3afd0720be77dbcbb9599fa66a24b67 /2020/01/puzzles.c |
Initial commit
Diffstat (limited to '2020/01/puzzles.c')
-rw-r--r-- | 2020/01/puzzles.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/2020/01/puzzles.c b/2020/01/puzzles.c new file mode 100644 index 0000000..29384e0 --- /dev/null +++ b/2020/01/puzzles.c @@ -0,0 +1,39 @@ +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#define BUFFER 200 + +int +main(void) +{ + int nums[BUFFER]; + FILE *fp; + + if (!(fp = fopen("input", "r"))) + err(EXIT_FAILURE, "fopen"); + + for (int i = 0; i < BUFFER; i++) + fscanf(fp, "%d", &nums[i]); + + fclose(fp); + + /* Inefficient, but small sample size so it's fine */ + for (int i = 0; i < BUFFER - 1; i++) { + for (int j = i + 1; j < BUFFER; j++) { +#ifdef PART2 + for (int k = j + 1; k < BUFFER; k++) { + if (nums[i] + nums[j] + nums[k] == 2020) { + printf("%d\n", nums[i] * nums[j] * nums[k]); + return EXIT_SUCCESS; + } + } +#else + if (nums[i] + nums[j] == 2020) { + printf("%d\n", nums[i] * nums[j]); + return EXIT_SUCCESS; + } +#endif + } + } +} |