diff options
author | Ben Harris <ben@tilde.team> | 2021-12-03 13:25:50 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2021-12-03 13:25:50 -0500 |
commit | 28eb4378a2a7c1d636d5cb26526d428a1ee9c2a6 (patch) | |
tree | 4943697fb9d563fbed4b31184199fedca4dbace0 /aoc2021/Extensions.cs | |
parent | 72e81981a349096c6925cf8b162f513ec9d32da0 (diff) |
use BigInteger for large inputs
also don't calculate MostCommon inside .Where()
Diffstat (limited to 'aoc2021/Extensions.cs')
-rw-r--r-- | aoc2021/Extensions.cs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/aoc2021/Extensions.cs b/aoc2021/Extensions.cs index 44202c4..84587a4 100644 --- a/aoc2021/Extensions.cs +++ b/aoc2021/Extensions.cs @@ -1,4 +1,6 @@ -namespace aoc2021; +using System.Numerics; + +namespace aoc2021; public static class Extensions { @@ -23,4 +25,21 @@ public static class Extensions /// <returns></returns> public static bool Contains(this Range range, int i) => i >= range.Start.Value && i <= range.End.Value; + + /// <summary> + /// Creates a new BigInteger from a binary (Base2) string + /// <see href="https://gist.github.com/mjs3339/73042bc0e717f98796ee9fa131e458d4" /> + /// </summary> + 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; + } } |