advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs | README | LICENSE

main.c (968B)


      1 #include <stdio.h>
      2 #include <time.h>
      3 #include <string.h>
      4 
      5 void puzzle(const char *filename, size_t *res1, size_t *res2);
      6 void puzzle_brute(const char *filename, size_t *res1, size_t *res2);
      7 
      8 int main(int argc, char *argv[]) {
      9 	printf("Advent of Code: day 07\n");
     10 	double time_start = clock();
     11 
     12 	if (argc <= 0) {
     13 		return -1;
     14 	}
     15 
     16 	if (argc == 1) {
     17 		printf("Usage: %s inputfile.txt\n", argv[0]);
     18 		return -1;
     19 	}
     20 
     21 	const char *filename = argv[1];
     22 	int is_brute = 0;
     23 
     24 	if (argc == 2) {
     25 		filename = argv[1];
     26 	} else { // > 2
     27 		is_brute = strlen(argv[1]) == 2 && strncmp(argv[1], "-b", 2) == 0;
     28 		filename = argv[2];
     29 	}
     30 
     31 	size_t counter1 = 0;
     32 	size_t counter2 = 0;
     33 
     34 	if (is_brute) {
     35 		puzzle_brute(filename, &counter1, &counter2);
     36 	} else {
     37 		puzzle(filename, &counter1, &counter2);
     38 	}
     39 
     40 	printf("Puzzle #1: %zu\n", counter1);
     41 	printf("Puzzle #2: %zu\n", counter2);
     42 
     43 	double elapsed = clock() - time_start;
     44 	printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC);
     45 
     46 	return 0;
     47 }