-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem18.cs
34 lines (29 loc) · 989 Bytes
/
Problem18.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.IO;
class Problem18
{
static void Main(string[] args)
{
// read in the triangle data from file and parse in to jagged array structure
string[] lines = System.IO.File.ReadAllLines("problem18_data.txt");
int[][] triangle = new int[lines.Length][];
for (int i = 0; i < lines.Length; i++)
{
string[] values = lines[i].Split(' ');
triangle[i] = new int[values.Length];
for (int j = 0; j < values.Length; j++)
{
triangle[i][j] = int.Parse(values[j]);
}
}
// Starting from the bottom of the tree move up adding sequentially
for (int i = triangle.Length - 1; i > 0; i--)
{
for (int j = 0; j < triangle[i].Length - 1; j++)
{
triangle[i - 1][j] += Math.Max(triangle[i][j], triangle[i][j + 1]);
}
}
Console.WriteLine(triangle[0][0]);
}
}