using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
public void Main(string[] args)
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 53 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233";
var inputLines = testFile.Split('\n');
var tree = new TreeCalculator();
foreach (var row in inputLines)
var numberRow = row.Trim().Replace(" "," ")
.Split(' ').Where(x=> Regex.IsMatch(x, @"^\d+$"))
.Select(x => int.Parse(x.Trim())).ToArray();
tree.pushValues(numberRow);
var maxPathSum = tree.calculateMax();
Console.WriteLine("Result is :" + maxPathSum );
public Node(int itself, int index)
public int index { get; set; }
public bool isPrime { get; set; }
public int itself { get; set; }
public Node leftSub { get; set; }
public Node rightSub { get; set; }
public int sum { get; set; }
static bool IsPrime(int number)
if (number == 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0) return false;
private List<Node> currentNodeList { get; set; }
public int calculateMax()
return currentNodeList.Where(x=>x.isPrime==false).Max(x => x.sum);
public void pushValues(int[] values)
rootNode = new Node(values[0], 0);
currentNodeList = new List<Node>();
currentNodeList.Add(rootNode);
var tempList = new List<Node>();
foreach (var node in currentNodeList)
var leftValue = values[node.index];
node.leftSub = new Node(leftValue, node.index);
if (node.isPrime || IsPrime(leftValue))
node.leftSub.isPrime = true;
node.leftSub.sum = node.sum + leftValue;
var rightValue = values[node.index + 1];
node.rightSub = new Node(rightValue, node.index + 1);
if (node.isPrime || IsPrime(rightValue))
node.rightSub.isPrime = true;
node.rightSub.sum = node.sum + rightValue;
tempList.Add(node.leftSub);
tempList.Add(node.rightSub);
currentNodeList = tempList;