language: C# (mono-2.8)
date: 179 days 4 hours ago
link:
visibility: private
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
using System;
using System.Text;
 
namespace Pyramid
{
    public class Pyramid
    {
            private readonly int rows;
            private readonly int[,] data;
 
            public Pyramid(int[,] data)
            {
                    this.data = data;
                    rows = data.GetLength(0);
            }
 
            public Pyramid(int rows)
            {
                    this.rows = rows;
                    data = new int[rows, rows];
            }
 
            public int this[int row, int col]
            {
                    get { return data[row, col]; }
                    set { data[row, col] = value; }
            }
 
            public int Rows
            {
                    get { return rows; }
            }
 
            public override string ToString()
            {
                    var sb = new StringBuilder();
 
                    for (int row = 0; row < rows; row++)
                    {
                            sb.Append(new string(' ', 9 * row / 2));
                            for (int col = 0; col < rows - row; col++)
                            {
                                    sb.AppendFormat("[{0:00000}] ", data[row, col]);
                            }
                            sb.AppendLine();
                    }
 
                    return sb.ToString();
            }
    }
 
    public interface IPyramidGenerator
    {
            Pyramid GeneratePyramid();
    }
 
    public class RandomPyramidGenerator : IPyramidGenerator
    {
            private readonly int rows;
            private readonly int range;
            private readonly Random random;
 
            public RandomPyramidGenerator(int rows, int range)
            {
                    this.rows = rows;
                    this.range = range;
                    random = new Random();
            }
 
            public Pyramid GeneratePyramid()
            {
                    var pyramid = new Pyramid(rows);
                    for (int row = 0; row < rows; row++)
                    {
                            for (int col = 0; col < rows - row; col++)
                            {
                                    pyramid[row, col] = random.Next(1, range);
                            }
                    }
                    return pyramid;
            }
    }
 
    public interface IPyramidSolver
    {
            long PyramidMaximumTotal(Pyramid pyramid);
    }
 
    // There is something wrong here. A few things actually... 
    public class NaivePyramidSolver : IPyramidSolver //...........................
    {
            public long PyramidMaximumTotal(Pyramid pyramid)
            {
                    return GetTotalAbove(pyramid.Rows - 1, 0, pyramid);
            }
 
            private long GetTotalAbove(int row, int column, Pyramid pyramid)
            {
                    if (row == 0) return 0;
 
                    int myValue = pyramid[row, column];
                    long left = myValue + GetTotalAbove(row - 1, column, pyramid);
                    long right = myValue + GetTotalAbove(row - 1, column + 1, pyramid);
                    return Math.Max(left, right);
            }
    }
 
 
    public class OurProgram
    {
            private static void Main()
            {
                    // this is doesn't get correct number
                    var generator = new RandomPyramidGenerator(5, 99);
                    Pyramid pyramid = generator.GeneratePyramid();
                    Console.WriteLine(pyramid);
 
                    var solver = new NaivePyramidSolver();
 
                    Console.WriteLine("This result is wrong, do you know why ?");
                    Console.WriteLine(solver.PyramidMaximumTotal(pyramid));
 
 
                    Console.ReadLine();
            }
    }
}