From 9400f9e65e03dfa974980159297b02847fb9b721 Mon Sep 17 00:00:00 2001 From: Gustavo Heinz Date: Sat, 20 Jun 2020 11:01:00 -0300 Subject: [PATCH] Finished 18, clean final print of 16 and 17 --- src/016.c | 2 +- src/017.c | 2 +- src/018.c | 94 ++++++++++++++++++++++++++++++++----------------------- 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/016.c b/src/016.c index 61d3f4e..379e173 100644 --- a/src/016.c +++ b/src/016.c @@ -19,5 +19,5 @@ int power_digit_sum(int base, int power) { int main() { assert(power_digit_sum(2,15) == 26); - printf("015: %d\n", power_digit_sum(2,1000)); + printf("016: %d\n", power_digit_sum(2,1000)); } diff --git a/src/017.c b/src/017.c index f6fcc08..81ced40 100644 --- a/src/017.c +++ b/src/017.c @@ -87,5 +87,5 @@ int main() { assert(number_letter_count(1000) == 11); assert(number_letter_count(342) == 23); assert(number_letter_count(115) == 20); - printf("016: %d\n", sum_letter_count_until_n(1000)); + printf("017: %d\n", sum_letter_count_until_n(1000)); } diff --git a/src/018.c b/src/018.c index acd4756..a63279b 100644 --- a/src/018.c +++ b/src/018.c @@ -1,51 +1,65 @@ #include -#include -#include -#include "lib/math.h" - -int triangle_path(int x, int y) { - if (x > y) - x = y; - return nth_triangular(y) + x; -} -int maximum_path_sum(int *triangle, size_t size) { - int i, path; - for(i=0; i right) ? left : right; + } + } + for (index = 0; index <= count; index++) { + lastRow[index] = currentRow[index]; } - return 0; } +int main(void) { + int maxSum = 0; + int temp = 0; + int row_index = 0; + int i; + char c; + FILE *fp; -void populate_triangle(int *triangle, char filename[]) { - FILE *file = fopen(filename, "r"); - if (file == NULL) { - printf("Failed to open file"); - exit(1); - } - char c,digit[2]; - int i = 0; - int digit_index = 0; - while ((c = fgetc(file)) != EOF) { - if (c == ' ' || c == '\n') { - triangle[i++] = atoi(digit); - digit_index = 0; + fp = fopen("src/resources/018_triangle.txt", "r"); + + while ((c = fgetc(fp)) != EOF) { + if (c == '\n') { + currentRow[row_index] = temp; + processRow(row_index); + temp = 0; + row_index = 0; + } else if (c == ' ') { + currentRow[row_index] = temp; + temp = 0; + row_index++; } else { - digit[digit_index++] = c; + /* Multiply current digit by ten. Since we are iterating one char + at the time we will receive a 7 and then a 5, so we need to turn + a 7 to 70 before adding 5 */ + temp *= 10; + temp += (c - '0'); // convert ascii char to integer } } -} + currentRow[row_index] = temp; + processRow(row_index); -int main() { - assert(nth_triangular(4) == 10); - - int* triangle = calloc(nth_triangular(15), - sizeof(int)); - populate_triangle(triangle, "src/resources/018_triangle.txt"); - maximum_path_sum(triangle, 3); - /* assert(maximum_path_sum(triangle, 3) == 393600); */ - /* printf("018: %d\n", maximum_path_sum(triangle, 15)); */ - free(triangle); + for (i=0; i<=MAX_SIZE; i++) { + if (maxSum < lastRow[i]) { + maxSum = lastRow[i]; + } + } + printf("018: %d", maxSum); } -- 2.45.2