using System.Numerics; namespace aoc2021; public static class Extensions { /// /// increased accuracy for stopwatch based on frequency. /// /// blog /// details here /// /// /// /// public static double ScaleMilliseconds(this Stopwatch stopwatch) => 1_000 * stopwatch.ElapsedTicks / (double)Stopwatch.Frequency; /// /// Does a include a given int? /// /// /// /// public static bool Contains(this Range range, int i) => i >= range.Start.Value && i <= range.End.Value; /// /// Creates a new BigInteger from a binary (Base2) string /// /// public static BigInteger BigIntegerFromBinaryString(this string binaryValue) { BigInteger res = 0; if (binaryValue.Count(b => b == '1') + binaryValue.Count(b => b == '0') != binaryValue.Length) return res; foreach (var c in binaryValue) { res <<= 1; res += c == '1' ? 1 : 0; } return res; } public static IEnumerable> Permute(this IEnumerable list) { var array = list as T[] ?? list.ToArray(); return array.Length == 1 ? new[] { array } : array.SelectMany(t => Permute(array.Where(x => !x!.Equals(t))), (v, p) => p.Prepend(v)); } public static int Pow(this int i, int power) { var pow = (uint) power; var ret = 1; while (pow != 0) { if ((pow & 1) == 1) ret *= i; i *= i; pow >>= 1; } return ret; } public static IEnumerable> Indexed(this IEnumerable source) { return source.Select((t, i) => new KeyValuePair(i, t)); } public static IEnumerable> WhereValue(this IEnumerable> source, Func func) { return source.Where(pair => func(pair.Value)); } }