Exemple #1
0
        static IEnumerable <HashAnalysisResult> BenchmarkNastyNestedCases()
        {
            var manuals = (
                from a in MoreEnumerable.Generate(-10000, a => a + 6).TakeWhile(a => a < 14100)
                from b0 in MoreEnumerable.Generate(int.MaxValue, c => (int)(c * 0.95)).TakeWhile(c => c > 0)
                from sign in new[] { -1, 1 }
                let b = sign * b0
                        select new NastyNestedManual {
                Nested = new NastyNestedManual {
                    A = b,
                    B = a,
                },
                A = a,
                B = b,
            }
                ).ToArray();

            var valueObjects = manuals.Select(m => m.ToValueObject()).ToArray();
            var tuples       = manuals.Select(m => m.ToTuple()).ToArray();
            var cs7tuples    = manuals.Select(m => m.ToCs7Tuple()).ToArray();

            yield return(ProcessList(manuals));

            yield return(ProcessList(valueObjects));

            yield return(ProcessList(tuples));

            yield return(ProcessList(cs7tuples));
        }
Exemple #2
0
        static IEnumerable <HashAnalysisResult> BenchmarkSymmetricalCases()
        {
            var manuals = (
                from a in MoreEnumerable.Generate(-5100, a => a + 5).TakeWhile(a => a < 5100)
                from b0 in MoreEnumerable.Generate(int.MaxValue, c => (int)(c * 0.95)).TakeWhile(c => c > 0)
                from sign in new[] { -1, 1 }
                let b = sign * b0
                        from swap in new[] { true, false }
                select new IntPairManual {
                A = swap ? a : b,
                B = swap ? b : a,
            }
                ).Distinct().ToArray();

            var valueObjects = manuals.Select(m => m.ToValueObject()).ToArray();
            var tuples       = manuals.Select(m => m.ToTuple()).ToArray();
            var structs      = manuals.Select(m => m.ToStruct()).ToArray();
            var anonymous    = manuals.Select(m =>
                                              new {
                m.A,
                m.B,
            }).ToArray();

            yield return(ProcessList(manuals));

            yield return(ProcessList(valueObjects));

            yield return(ProcessList(tuples));

            yield return(ProcessList(structs));

            yield return(ProcessList(anonymous));
        }
Exemple #3
0
        static IEnumerable <HashAnalysisResult> BenchmarkIntPairCases()
        {
            var manuals = (
                from a in MoreEnumerable.Generate(-8000, a => a + 4).TakeWhile(a => a < 8000)
                from b0 in MoreEnumerable.Generate(int.MaxValue, c => (int)(c * 0.95)).TakeWhile(c => c > 0)
                from sign in new[] { -1, 1 }
                let b = sign * b0
                        select new IntPairManual {
                A = a,
                B = b,
            }
                ).ToArray();

            var valueObjects = manuals.Select(m => m.ToValueObject()).ToArray();
            var tuples       = manuals.Select(m => m.ToTuple()).ToArray();
            var cs7tuples    = manuals.Select(m => m.ToCs7Tuple()).ToArray();
            var structs      = manuals.Select(m => m.ToStruct()).ToArray();
            var anonymous    = manuals.Select(m =>
                                              new {
                m.A,
                m.B,
            }).ToArray();

            yield return(ProcessList(manuals));

            yield return(ProcessList(valueObjects));

            yield return(ProcessList(tuples));

            yield return(ProcessList(cs7tuples));

            yield return(ProcessList(structs));

            yield return(ProcessList(anonymous));
        }
            public void Solve()
            {
                var salt = ReadLine();

                void InitArrays()
                {
                    _mpValToIndexRepeating5 = Enumerable.Repeat(-1, 16).ToList();
                    _nextCheck = 0;
                    _md5Cache  = new List <byte[]>(23000);
                }

                InitArrays();

                WriteLine(MoreEnumerable
                          .Generate(0, i => i + 1)
                          .Where(n => IsKey(salt, n, false))
                          .Take(64)
                          .Last());

                InitArrays();
                Write(MoreEnumerable
                      .Generate(0, i => i + 1)
                      .Where(n => IsKey(salt, n, true))
                      .Take(64)
                      .Last());

                _md5Cache = null;                       // Free up this memory
            }
Exemple #5
0
        public void Refresh()
        {
            bool hasRows = _rowDefinitions.Count > 0;
            bool hasCols = _columnDefinitions.Count > 0;
            int  rCount  = Math.Max(_rowDefinitions.Count, 1);
            int  cCount  = Math.Max(_columnDefinitions.Count, 1);
            var  g       = new List <UIElement> [rCount, cCount];
            var  rc      = MoreEnumerable.Generate(true, b => b).Take(rCount).ToArray();
            var  cc      = MoreEnumerable.Generate(true, b => b).Take(cCount).ToArray();

            for (int i = 0; i < rCount; i++)
            {
                for (int j = 0; j < cCount; j++)
                {
                    g[i, j] = new List <UIElement>();
                }
            }

            for (int k = 0; k < _grid.Children.Count; k++)
            {
                var c = _grid.Children[k];
                int i = (int)(c.GetValue(Grid.RowProperty) ?? 0);
                int j = (int)(c.GetValue(Grid.ColumnProperty) ?? 0);

                g[i, j].Add(c);
            }

            for (int i = 0; i < rCount; i++)
            {
                for (int j = 0; j < cCount; j++)
                {
                    var hasContent = g[i, j].Any(HasContent);

                    rc[i] = rc[i] && hasContent;
                    cc[j] = cc[j] && hasContent;
                }
            }

            if (hasRows)
            {
                for (int i = 0; i < rCount; i++)
                {
                    _grid.RowDefinitions[i] = rc[i] ? CloneRow(i) : EmptyRow;
                }
            }

            if (hasCols)
            {
                for (int j = 0; j < cCount; j++)
                {
                    _grid.ColumnDefinitions[j] = cc[j] ? CloneColumn(j) : EmptyColumn;
                }
            }
        }
Exemple #6
0
        public void UnfoldFiniteSequence()
        {
            var result = MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1),
                                               e => e.Result <= 100,
                                               e => e.State,
                                               e => e.Result);

            var expectations = MoreEnumerable.Generate(1, x => x + 1).Take(100);

            Assert.That(result, Is.EqualTo(expectations));
        }
Exemple #7
0
        public void UnfoldFiniteSequence()
        {
            var result = MoreEnumerable.Unfold(1, x => new { result = x, state = x + 1 },
                                               e => e.result <= 100,
                                               e => e.state,
                                               e => e.result);

            var expectations = MoreEnumerable.Generate(1, x => x + 1).Take(100);

            Assert.That(result, Is.EqualTo(expectations));
        }
Exemple #8
0
        private void ShowInerDirtectiryRec(DirectoryInfo directoryInfo, int index)
        {
            Console.WriteLine($"{string.Concat(MoreEnumerable.Generate("\t", (s) => s).Take(index))}{directoryInfo.Name}");
            directoryInfo.GetDirectories().ForEach(d => ShowInerDirtectiryRec(d, index + 1));

            foreach (var f in directoryInfo.GetFiles())
            {
                if (FileSystemHelper.FileFormats.Where(format => f.Name.LastIndexOf(format) == f.Name.Length - format.Length).Any())
                {
                    Console.WriteLine($"{string.Concat(MoreEnumerable.Generate("\t", (s) => s).Take(index))}--f: {f}");
                }
            }
        }
Exemple #9
0
        public (string, string) Solve(string[] input)
        {
            var earliestLeavingTime = long.Parse(input[0]);
            var busses  = input[1].Split(',').Where(x => x != "x").Select(int.Parse).ToList();
            var leaving = MoreEnumerable.Generate(earliestLeavingTime, n => n + 1)
                          .Select(m => new { Min = m, Bus = busses.FirstOrDefault(b => m % b == 0) })
                          .First(x => x.Bus != 0);
            var part1 = (leaving.Min - earliestLeavingTime) * leaving.Bus;

            var part2 = Solve2Optimized(input[1]);

            return(part1.ToString(), part2.ToString());
        }
Exemple #10
0
 public static IQuery <int> Sequence(int first, int last, int step)
 {
     if (step <= 0)
     {
         throw new ArgumentException(null, nameof(step));
     }
     if (last < first)
     {
         step = -step;
     }
     return(MoreEnumerable.Generate(first, i => i + step)
            .TakeWhile(i => step < 0 ? i >= last : i <= last)
            .ToQuery());
 }
        public void ZipLongestDisposeSequencesEagerly()
        {
            var shorter = TestingSequence.Of(1, 2, 3);
            var longer  = MoreEnumerable.Generate(1, x => x + 1);
            var zipped  = shorter.ZipLongest(longer, Tuple.Create);

            var count = 0;

            foreach (var _ in zipped.Take(10))
            {
                if (++count == 4)
                {
                    ((IDisposable)shorter).Dispose();
                }
            }
        }
Exemple #12
0
        static IEnumerable <HashAnalysisResult> BenchmarkComplicatedCases()
        {
            var manuals = (
                from a in MoreEnumerable.Generate(-500, a => a + 4).TakeWhile(a => a < 500)
                from b0 in MoreEnumerable.Generate(int.MaxValue, b => (int)(b * 0.9)).TakeWhile(b => b > 0)
                from sign in new[] { -1, 1 }
                let b = sign * b0
                        from c in MoreEnumerable.Generate(1, c => c << 1).TakeWhile(c => c > 0)
                        select new ComplicatedManual {
                AnEnum = (SeekOrigin)(c % 3),
                A = a,
                NullableInt = a < b && b < c ? default(int?) : a & b & c,
                B = b,
                Label = Math.Min(a, c).ToString(),
                Time = new DateTime(2014, 1, 1) + TimeSpan.FromSeconds(a + c + Math.Log(b0)),
                C = c,
            }
                ).ToArray();
            var valueObjects = manuals.Select(m => m.ToValueObject()).ToArray();
            var tuples       = manuals.Select(m => m.ToTuple()).ToArray();
            var cs7tuples    = manuals.Select(m => m.ToCs7Tuple()).ToArray();
            var structs      = manuals.Select(m => m.ToStruct()).ToArray();
            var anonymous    = manuals.Select(m =>
                                              new {
                m.AnEnum,
                m.A,
                m.NullableInt,
                m.B,
                m.Label,
                m.Time,
                m.C
            }
                                              ).ToArray();

            yield return(ProcessList(manuals));

            yield return(ProcessList(valueObjects));

            yield return(ProcessList(tuples));

            yield return(ProcessList(cs7tuples));

            yield return(ProcessList(structs));

            yield return(ProcessList(anonymous));
        }
Exemple #13
0
        public void TransposeWithAllRowsAsInfiniteSequences()
        {
            var matrix = MoreEnumerable.Generate(1, x => x + 1)
                         .Where(IsPrime)
                         .Take(3)
                         .Select(x => MoreEnumerable.Generate(x, n => n * x));

            var result = matrix.Transpose().Take(5);

            var expectations = new[]
            {
                new[] { 2, 3, 5 },
                new[] { 4, 9, 25 },
                new[] { 8, 27, 125 },
                new[] { 16, 81, 625 },
                new[] { 32, 243, 3125 }
            };

            AssertMatrix(expectations, result);
        }
        protected override void ExecuteDay(byte[] input)
        {
            if (input == null)
            {
                return;
            }

            var regex      = new Regex(@"^Generator (?<gen>\w) starts with (?<init>\d+)$", RegexOptions.Compiled);
            var generators = input.GetLines()
                             .Select(l => regex.Match(l))
                             .ToDictionary(
                m => m.Groups["gen"].Value,
                m => (ulong)Convert.ToInt32(m.Groups["init"].Value));

            Dump('A',
                 Enumerable.Range(0, 40_000_000)
                 .Scan(
                     (a: generators["A"], b: generators["B"]),
                     (state, _) => ((state.a * 16807) % 2147483647, (state.b * 48271) % 2147483647))
                 .Skip(1)
                 .Where(s => (ushort)s.a == (ushort)s.b)
                 .Count());

            var aGenerator = MoreEnumerable
                             .Generate(generators["A"], a => (a * 16807) % 2147483647)
                             .Where(a => a % 4 == 0);
            var bGenerator = MoreEnumerable
                             .Generate(generators["B"], b => (b * 48271) % 2147483647)
                             .Where(b => b % 8 == 0);

            Dump('B',
                 aGenerator.Zip(bGenerator, (a, b) => (a, b))
                 .Take(5_000_000)
                 .Where(s => (ushort)s.a == (ushort)s.b)
                 .Count());
        }
 static IEnumerable <int> fuelValues(int start) =>
 MoreEnumerable.Generate(start, s => Math.Max(s / 3 - 2, 0))
 .Skip(1)
 .TakeWhile(s => s != 0);
        protected override void ExecuteDay(byte[] input)
        {
            var marbles = new LinkedList <int>();

            LinkedListNode <int> prevNode(LinkedListNode <int> node) =>
            node.Previous ?? marbles.Last;
            LinkedListNode <int> nextNode(LinkedListNode <int> node) =>
            node.Next ?? marbles.First;

            LinkedListNode <int> nextNodeCount(LinkedListNode <int> node, int count)
            {
                for (int i = 0; i < count; i++)
                {
                    node = nextNode(node);
                }
                return(node);
            }

            void AddNumber(int number)
            {
                foreach (var n in MoreEnumerable
                         .Generate(
                             number,
                             n => n / 10)
                         .TakeWhile(n => n != 0)
                         .Reverse()
                         .Select(n => n % 10)
                         .DefaultIfEmpty(0))
                {
                    marbles.AddLast(n);
                }
            }

            AddNumber(37);

            var elf1 = marbles.First;
            var elf2 = nextNode(elf1);

            void DoTick()
            {
                AddNumber(elf1.Value + elf2.Value);

                elf1 = nextNodeCount(elf1, elf1.Value + 1);
                elf2 = nextNodeCount(elf2, elf2.Value + 1);
            }

            var numRecipes = Convert.ToInt32(input.GetString());

            while (marbles.Count < numRecipes + 10)
            {
                DoTick();
            }

            Dump('A',
                 string.Join(
                     "",
                     MoreEnumerable
                     .Generate(
                         marbles.First,
                         n => nextNode(n))
                     .Skip(numRecipes)
                     .Take(10)
                     .Select(n => n.Value)));

            marbles.Clear();
            AddNumber(numRecipes);
            var matchList = marbles.Reverse().ToList();

            marbles.Clear();
            AddNumber(37);
            elf1 = marbles.First;
            elf2 = nextNode(elf1);

            while (true)
            {
                DoTick();

                if (MoreEnumerable
                    .Generate(
                        marbles.Last,
                        n => prevNode(n))
                    .Take(matchList.Count)
                    .Select(n => n.Value)
                    .SequenceEqual(matchList))
                {
                    Dump('B', marbles.Count - matchList.Count);
                    return;
                }

                if (MoreEnumerable
                    .Generate(
                        marbles.Last,
                        n => prevNode(n))
                    .Skip(1)
                    .Take(matchList.Count)
                    .Select(n => n.Value)
                    .SequenceEqual(matchList))
                {
                    Dump('B', marbles.Count - matchList.Count - 1);
                    return;
                }
            }
        }
Exemple #17
0
 public void GenerateWithNullGenerator()
 {
     MoreEnumerable.Generate(0, null);
 }
Exemple #18
0
 public void GenerateIsLazy()
 {
     MoreEnumerable.Generate(0, BreakingFunc.Of <int, int>());
 }
Exemple #19
0
        public void GenerateProcessesNonNumerics()
        {
            var result = MoreEnumerable.Generate("", s => s + 'a').TakeWhile(s => s.Length < 5);

            result.AssertSequenceEqual("", "a", "aa", "aaa", "aaaa");
        }
Exemple #20
0
        public void GenerateTerminatesWhenCheckReturnsFalse()
        {
            var result = MoreEnumerable.Generate(1, n => n + 2).TakeWhile(n => n < 10);

            result.AssertSequenceEqual(1, 3, 5, 7, 9);
        }
Exemple #21
0
 public void GenerateWithNullGenerator()
 {
     Assert.ThrowsArgumentNullException("generator", () =>
                                        MoreEnumerable.Generate(0, null));
 }
Exemple #22
0
        public void GenerateIsLazy()
        {
            var result = MoreEnumerable.Generate(0, BreakingFunc.Of <int, int>()).TakeWhile(n => false);

            result.Consume();
        }
Exemple #23
0
 public void GenerateFuncIsNotInvokedUnnecessarily()
 {
     MoreEnumerable.Generate(0, BreakingFunc.Of <int, int>())
     .Take(1)
     .Consume();
 }