M src/016.c => src/016.c +1 -1
@@ 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));
}
M src/017.c => src/017.c +1 -1
@@ 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));
}
M src/018.c => src/018.c +54 -40
@@ 1,51 1,65 @@
#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#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<size; i++) {
- path = triangle_path(i, i);
- printf("%d %d\n", path, triangle[path]);
+#define MAX_SIZE 15
+
+int lastRow[MAX_SIZE] = {0};
+int currentRow[MAX_SIZE] = {0};
+
+// Process rows by collapsing them into the previous row
+void processRow(int count) {
+ int index, left, right;
+
+ for (index = 0; index <= count; index++) {
+ if (index == 0) {
+ currentRow[index] += lastRow[index];
+ } else if (index == count) {
+ currentRow[index] += lastRow[index - 1];
+ } else {
+ left = currentRow[index] + lastRow[index - 1];
+ right = currentRow[index] + lastRow[index];
+ currentRow[index] = (left > 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);
}