Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MatrixExercise
- {
- class Program
- {
- static void Main(string[] args)
- {
- var sw = Stopwatch.StartNew();
- var union = new HashSet<Int32>();
- var result = new HashSet<Int32>();
- foreach (var row in GetRowFromFile("Matrix.txt"))
- {
- var copy = new int[row.Count];
- row.CopyTo(copy);
- row.IntersectWith(union);
- union.UnionWith(copy);
- result.UnionWith(row);
- }
- sw.Stop();
- Console.WriteLine("Time: " + sw.Elapsed);
- foreach (var i in result)
- {
- Console.WriteLine(i);
- }
- Console.ReadKey();
- }
- static IEnumerable<HashSet<int>> GetRowFromFile(string path)
- {
- return ReadLines(() => File.OpenText(path), ParseLine);
- }
- private static HashSet<Int32> ParseLine(string line)
- {
- var numbers = line.Split(' ');
- var intList = new HashSet<int>(numbers.Select(Int32.Parse));
- return intList;
- }
- static IEnumerable<T> ReadLines<T>(Func<TextReader> provider, Func<string, T> morpher)
- {
- using (var reader = provider())
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- yield return morpher(line);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement