Пример #1
0
        public static List <Triangle> Parse(string line)
        {
            var triangles = new List <Triangle>();
            var strSides  = line.Split(new char[] { ' ' },
                                       StringSplitOptions.RemoveEmptyEntries);

            if (strSides.Length < 3)
            {
                return(triangles);
            }
            var sides = new int[strSides.Length];

            // Convert to int array
            for (int i = 0; i < strSides.Length; i++)
            {
                sides[i] = int.Parse(strSides[i]);
            }

            for (int i = 0; i < sides.Length; i += 3)
            {
                var triangle = new Triangle()
                {
                    A = sides[i],
                    B = sides[i + 1],
                    C = sides[i + 2]
                };
                if (TriangleValidator.IsValid(triangle))
                {
                    triangles.Add(triangle);
                }
            }
            return(triangles);
        }
Пример #2
0
        static void Main(string[] args)
        {
            string input     = File.ReadAllText("input.txt");
            var    triangles = new List <Triangle>();

            foreach (string line in new ColumnLineReader(input))
            {
                var parsed = TriangleParser.Parse(line);
                foreach (var triangle in parsed)
                {
                    if (TriangleValidator.IsValid(triangle))
                    {
                        triangles.Add(triangle);
                    }
                }
            }
            Console.WriteLine("Found {0} valid triangles", triangles.Count);
            Console.ReadLine();
        }