Exemplo n.º 1
0
        public override object Solution()
        {
            // a quick look-up table of which perimeters (values of P) have been found and how frequently
            Dictionary<int, int> counter = new Dictionary<int, int>();

            // we'll go ahead and check A and B to 500 even though that would never work (e.g. 500^2 + 500^2 = C^2 ??)
            foreach (int a in Enumerable.Range(1, 500))
            {
                foreach (int b in Enumerable.Range(1, 500))
                {
                    var t = new Triangle()
                                {
                                    A = a,
                                    B = b
                                };

                    // if this is a valid right triangle and the perimeter is less than 1000 as described
                    if (t.Check && t.P <= 1000)
                    {
                        if (!counter.ContainsKey(t.P))
                        {
                            counter.Add(t.P, 0);
                        }

                        counter[t.P] += 1;
                    }
                }
            }

            return counter.OrderByDescending(t => t.Value).First().Key;
        }
Exemplo n.º 2
0
        int GetTriangleMaxSum(Triangle t)
        {
            if (t == null)
                return 0;
            if (maxSums.ContainsKey(t))
                return maxSums[t];

            if (t.leftClosed || t.left == null)
                return t.value + GetTriangleMaxSum(t.right);
            if (t.rightClosed || t.right == null)
                return t.value + GetTriangleMaxSum(t.left);

            if (t.left.value > t.right.value)
                t.right.leftClosed = true;
            else
                t.left.rightClosed = true;

            int leftSum = GetTriangleMaxSum(t.left);
            int rightSum = GetTriangleMaxSum(t.right);
            int s = t.value + (leftSum > rightSum ? leftSum : rightSum);
            maxSums.Add(t, s);

            return s;
        }
Exemplo n.º 3
0
        public Triangle(List<int[]> lines)
        {
            List<Triangle> lastTriangleLine = new List<Triangle>();
            for (int i = lines.Count - 1; i > 0; i--)
            {
                List<Triangle> nextTriangleLine = new List<Triangle>();
                for (int j = 0; j <= i; j++)
                {
                    Triangle t = new Triangle(lines[i][j]);
                    if (i < lines.Count - 1)
                    {
                        t.left = lastTriangleLine[j];
                        t.right = lastTriangleLine[j + 1];
                    }

                    nextTriangleLine.Add(t);
                }

                lastTriangleLine = new List<Triangle>(nextTriangleLine);
            }

            value = lines[0][0];
            left = lastTriangleLine[0];
            right = lastTriangleLine[1];
        }
Exemplo n.º 4
0
 public Triangle(int value, Triangle left, Triangle right)
 {
     this.value = value;
     this.left = left;
     this.right = right;
 }
Exemplo n.º 5
0
        public static Triangle Read(List<int[]> lines, int currentLine, int currentPos, int size)
        {
            Triangle t = new Triangle(lines[currentLine][currentPos]);
            if (size == 1)
                return t;
            t.left = Triangle.Read(lines, currentLine + 1, currentPos, size - 1);
            t.right = Triangle.Read(lines, currentLine + 1, currentPos + 1, size - 1);

            return t;
        }