aboutsummaryrefslogtreecommitdiff
path: root/2020/01/puzzles.c
blob: 29384e0fae160936291bb4e0efe28557603fa743 (plain) (blame)
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
#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
		}
	}
}