[2016-05-09] Challenge #266 [Easy] Basic Graph Statistics: Node Degrees

C#: using System; using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Xml.Linq;

        namespace DailyProgrammer_NodeGraph
        {
            /* https://www.reddit.com/r/dailyprogrammer/comments/4ijtrt/20160509_challenge_266_easy_basic_graph/ */
            public class Program
            {
                static void Main(string[] args)
                {
                    var test = new NodeGraph();
                    HoldClose();

                }
                static void HoldClose()
                {
                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();
                }
            }
            public class NodeGraph
            {
                private List<Node> Graph = new List<Node>();
                public NodeGraph()
                {
                    GetInput();
                    PrintAll();
                }
                /* send the NodeID to add to the node passed */
                public void AddEdgeToGraph(int edgeID, int nodeID)
                {
                    var edge = Graph.Find(e => e.Equals(edgeID));
                    if (edge != null)
                    {
                        Graph.Find(q => q.Equals(nodeID)).AddEdge(edge);
                    }
                }
                private void GetInput()
                {
                    var directory = Directory.GetCurrentDirectory();
                    directory = directory + @"\input.txt";
                    Console.WriteLine("Current Input file: {0}", directory);
                    var r = new StreamReader(directory);
                    try
                    {
                        var max = int.Parse(r.ReadLine());
                        InitNodes(max);

                        var split = r.ReadLine();
                        while (split != null)
                        {
                            var text = split.Split(' ');
                            var id1 = int.Parse(text[0]);
                            var id2 = int.Parse(text[1]);
                            AddEdgeToGraph(id2, id1);
                            split = r.ReadLine();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                private void InitNodes(int numNodes)
                {
                    for (var i = 1; i <= numNodes; i++)
                    {
                        var n = new Node(i);
                        Graph.Add(n);
                    }
                }
                private void PrintAll()
                {
                    foreach (var n in Graph)
                    {
                        n.PrintAll();
                    }
                }
            }
            public class Node
            {
                private List<Node> _edges = new List<Node>();
                private int NodeId { get; set; }

                public Node(int nodeId)
                {
                    NodeId = nodeId;
                }

                public int GetCountOfEdges()
                {
                    return _edges.Count;
                }

                public bool Equals(int id)
                {
                    return NodeId == id;
                }

                public void AddEdge(Node n)
                {
                    _edges.Add(n);
                }
                public void PrintAll()
                {
                    if (_edges.Count > 0)
                    {
                        foreach (var e in _edges)
                        {
                            Console.WriteLine("Node: {0} has edge {1} ", NodeId, e.NodeId);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Node {0} has no edges.", NodeId);
                    }
                }
            }
        }
/r/dailyprogrammer Thread