-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2250a14
commit 675015a
Showing
9 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,3 +245,5 @@ _Pvt_Extensions | |
output.txt | ||
multipleSolutions.txt | ||
costs.txt | ||
|
||
*.Artifacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Numerics; | ||
|
||
namespace PtVzzlexMasCake.Tests | ||
{ | ||
[TestClass] | ||
public class BinaryPuzzleSolverTests | ||
{ | ||
[TestMethod] | ||
[DataRow(10)] | ||
[DataRow(100)] | ||
[DataRow(1000)] | ||
[DataRow(10000)] | ||
public void SolveBigNumber(int digits) | ||
{ | ||
var r = new Random(0); | ||
var s = new char[digits]; | ||
for (int i = 0; i < digits; i++) | ||
{ | ||
s[i] = r.Next(10).ToString().First(); | ||
} | ||
var value = BigInteger.Parse(new string(s)); | ||
|
||
var expectedSolution = PuzzleSolver.Solve(value); | ||
var solution = BinaryPuzzleSolver.Solve(value); | ||
CollectionAssert.AreEqual(expectedSolution, solution); | ||
|
||
var initialValue = value; | ||
foreach (var operation in solution) | ||
{ | ||
initialValue = Operator.Execute(operation, initialValue); | ||
} | ||
Assert.AreEqual(1, initialValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System.Numerics; | ||
|
||
namespace PtVzzlexMasCake | ||
{ | ||
public class Benchmark | ||
{ | ||
[Params(1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7, 1 << 8, 1 << 9, 1 << 10)] | ||
public int Digits; | ||
|
||
private BigInteger Value; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
var r = new Random(0); | ||
var s = new char[Digits]; | ||
for (int i = 0; i < Digits; i++) | ||
{ | ||
s[i] = r.Next(10).ToString().First(); | ||
} | ||
Value = BigInteger.Parse(new string(s)); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void PuzzleSolver() | ||
{ | ||
_ = PtVzzlexMasCake.PuzzleSolver.Solve(Value); | ||
} | ||
[Benchmark] | ||
public void BinaryPuzzleSolver() | ||
{ | ||
_ = PtVzzlexMasCake.BinaryPuzzleSolver.Solve(Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Numerics; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
|
||
namespace PtVzzlexMasCake | ||
{ | ||
internal static class BinaryPuzzleSolver | ||
{ | ||
static Func<BigInteger, uint[]?> GetBits = CreateGetter<BigInteger, uint[]?>(typeof(BigInteger).GetField("_bits", BindingFlags.NonPublic | BindingFlags.Instance)); | ||
static Func<BigInteger, int> GetSign = CreateGetter<BigInteger, int>(typeof(BigInteger).GetField("_sign", BindingFlags.NonPublic | BindingFlags.Instance)); | ||
|
||
public static List<Operation> Solve(BigInteger value) | ||
{ | ||
var solution = new List<Operation>(); | ||
while (!value.IsOne) | ||
{ | ||
if (value.IsEven) | ||
{ | ||
solution.Add(Operation.Div); | ||
value = value >> 1; | ||
continue; | ||
} | ||
|
||
if (value == 3) | ||
{ | ||
solution.Add(Operation.Sub); | ||
value = value - 1; | ||
continue; | ||
} | ||
var bits = GetBits(value); | ||
|
||
var firstBits = bits?[0] ?? (uint)GetSign(value); | ||
|
||
if ((firstBits & 3) == 3) | ||
{ | ||
solution.Add(Operation.Add); | ||
value = value + 1; | ||
} | ||
else | ||
{ | ||
solution.Add(Operation.Sub); | ||
value = value - 1; | ||
} | ||
} | ||
return solution; | ||
} | ||
static Func<S, T> CreateGetter<S, T>(FieldInfo field) | ||
{ | ||
string methodName = field.ReflectedType.FullName + ".get_" + field.Name; | ||
DynamicMethod getterrMethod = new DynamicMethod(methodName, typeof(T), new Type[1] { typeof(S) }, true); | ||
ILGenerator gen = getterrMethod.GetILGenerator(); | ||
if (field.IsStatic) | ||
{ | ||
gen.Emit(OpCodes.Ldsfld, field); | ||
} | ||
else | ||
{ | ||
gen.Emit(OpCodes.Ldarg_0); | ||
gen.Emit(OpCodes.Ldfld, field); | ||
} | ||
gen.Emit(OpCodes.Ret); | ||
return (Func<S, T>)getterrMethod.CreateDelegate(typeof(Func<S, T>)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters