Skip to content

Instantly share code, notes, and snippets.

@aymanfarhat
Created September 9, 2012 16:49
Show Gist options
  • Save aymanfarhat/3685587 to your computer and use it in GitHub Desktop.
Save aymanfarhat/3685587 to your computer and use it in GitHub Desktop.
Project Euler Problem 18
class Program
{
/* The triangle */
static int[][] triangle = null;
/* Saves respective optimal sums of nodes in the triangle */
static int[][] optimalSums = null;
static void Main(string[] args)
{
triangle = File.ReadLines("triangle.txt")
.Select(l => l.Split(' ').Select(n => int.Parse(n)).ToArray())
.ToArray();
optimalSums = new int[triangle.Length][];
for (int i = triangle.Length - 1; i >= 0; i--)
{
optimalSums[i] = new int[i + 1];
for (int j = 0; j < triangle[i].Length; j++)
{
optimalSums[i][j] = getOptimalSum(i, j);
}
}
Console.WriteLine(optimalSums[0][0]);
}
/* Gets optimal sum of a node from its position downward */
static int getOptimalSum(int i, int j)
{
if (i == (triangle.Length-1))
return triangle[i][j];
else
{
int sum1 = triangle[i][j] + optimalSums[i + 1][j];
int sum2 = triangle[i][j] + optimalSums[i + 1][j + 1];
return ( sum1 > sum2 )? sum1 : sum2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment