Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -0,0 +1,21 @@
# syntax: GAWK -f MAXIMUM_TRIANGLE_PATH_SUM.AWK filename(s)
{ printf("%s\n",$0)
cols[FNR] = NF
for (i=1; i<=NF; i++) {
arr[FNR][i] = $i
}
}
ENDFILE {
for (row=FNR-1; row>0; row--) {
for (col=1; col<=cols[row]; col++) {
arr[row][col] += max(arr[row+1][col],arr[row+1][col+1])
}
}
printf("%d using %s\n\n",arr[1][1],FILENAME)
delete arr
delete cols
}
END {
exit(0)
}
function max(x,y) { return((x > y) ? x : y) }

View file

@ -44,7 +44,7 @@ public program()
{
for(int j := 0, j < 18, j += 1)
{
list[i][j] := mathControl.max(list[i][j] + list[i+1][j], list[i][j] + list[i+1][j+1])
list[i][j] := max(list[i][j] + list[i+1][j], list[i][j] + list[i+1][j+1])
}
};

View file

@ -1,14 +1,17 @@
'''Maximum triangle path sum'''
from functools import (reduce)
from itertools import (starmap)
# maxPathSum :: [[Int]] -> Int
def maxPathSum(rows):
'''The maximum total among all possible
paths from the top to the bottom row.
'''
return reduce(
lambda xs, ys: list(starmap(
lambda a, b, c: a + max(b, c),
zip(ys, xs, xs[1:])
)),
lambda xs, ys: [
a + max(b, c) for (a, b, c) in zip(ys, xs, xs[1:])
],
reversed(rows[:-1]), rows[-1]
)[0]