From 9f17ae97ac8008c85748b0e344d2fc851d0a5c76 Mon Sep 17 00:00:00 2001 From: dfdx Date: Sun, 7 Jul 2024 22:38:40 -0400 Subject: [PATCH] Parse input file. --- 2015/day09/answer01.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2015/day09/answer01.c diff --git a/2015/day09/answer01.c b/2015/day09/answer01.c new file mode 100644 index 0000000..6997bd1 --- /dev/null +++ b/2015/day09/answer01.c @@ -0,0 +1,39 @@ +/* The simplest way I can think of solving this problem (i.e., the brute force method): + * 1. enter all cities, with their relative distances, into an "adjacency matrix" + * - in this particular case, we have a fully connected (i.e., "complete") graph. + * - the number of *unique* possible paths are 8!/2 = 20,160 + * 2. the cities are labeled 0 through 7. + * - hence the possible paths are different ways to arrange the numbers 0, 1, 2, 3, 4, 5, 6, 7. + * 3. construct a nested for-loop (or a recursive function) to walk through each node. + * 4. from an arbitrary start to an arbitrary end, mark down the length of the path. + * !. but how do you avoid reversed paths? |01234567| = |76543210|... + */ + +#include + + +int main() { + FILE *file = fopen("testinput", "r"); + if (file == NULL) { + perror("main: error opening file."); + return 1; + } + + + char city1[12]; + char city2[12]; + int dist = 0; + + + while (fscanf(file, "%s to %s = %d\n", city1, city2, &dist) != EOF) { + printf("%s, %s, %d\n", city1, city2, dist); + } + fclose(file); + + + return 0; +} + + + +