-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem018.cs
136 lines (117 loc) · 3.4 KB
/
Problem018.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace ProjectEuler.Solutions
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ProjectEuler.Input;
/// <summary>
/// Maximum path sum I.
/// By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
/// <para> 3 </para>
/// <para> 7 4 </para>
/// <para> 2 4 6 </para>
/// <para>8 5 9 3</para>
/// That is, 3 + 7 + 4 + 9 = 23.
/// <para>Find the maximum total from top to bottom of the triangle below.</para>
/// <b>NOTE</b>: As there are only 16384 routes, it is possible to solve this problem by trying every route.
/// However, <see cref="Problem067"/>, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
/// </summary>
/// <remarks>The digits are stored in the file <a href="Problem018.txt">Problem018.txt</a>.</remarks>
public class Problem018 : Problem
{
public override long Solution()
{
return GetMaximumPath(Input.Problem018);
}
private static long GetMaximumPath(string input)
{
byte[][] triangle = CreateTriangle(input);
Node root = Node.CreateTree(triangle);
return root.Value;
}
private static byte[][] CreateTriangle(string input)
{
string[] rows = input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
byte[][] triangle = new byte[rows.Length][];
for(int x = 0; x < rows.Length; x++)
{
string[] row = rows[x].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
triangle[x] = new byte[row.Length];
for(int y = 0; y < row.Length; y++)
{
triangle[x][y] = byte.Parse(row[y]);
}
}
return triangle;
}
private class Node
{
private readonly Node[] children = new Node[2];
private Node(long value)
{
this.Value = value;
}
public long Value { get; private set; }
public static Node CreateTree(byte[][] data, int x = 0, int y = 0, IDictionary<string, Node> knownNodes = null)
{
if (knownNodes == null)
{
knownNodes = new Dictionary<string, Node>();
}
string point = x + "|" + y;
Node node = knownNodes.ContainsKey(point)
? knownNodes[point]
: null;
if (node == null && x < data.Length && y < data[x].Length)
{
node = new Node(data[x][y]);
node.CreateChildNode(data, x + 1, y, knownNodes);
node.CreateChildNode(data, x + 1, y + 1, knownNodes);
long maxhChildValue = 0;
for (int i = 0; i < 2; i++)
{
Node childNode = node.children[i];
if (childNode != null && childNode.Value > maxhChildValue)
{
maxhChildValue = childNode.Value;
}
}
node.Value += maxhChildValue;
knownNodes.Add(point, node);
}
return node;
}
private void CreateChildNode(byte[][] data, int x, int y, IDictionary<string, Node> knownNodes)
{
Node childNode = CreateTree(data, x, y, knownNodes);
if (childNode != null)
{
if (this.children[0] == null)
{
this.children[0] = childNode;
}
else
{
this.children[1] = childNode;
}
}
}
}
[Test]
public void TestForExample()
{
const string smallTriangle = @"
3
7 4
2 4 6
8 5 9 3";
long maximumPath = GetMaximumPath(smallTriangle);
Assert.AreEqual(23, maximumPath);
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(1074, this.Solution());
}
}
}