Last active
March 11, 2023 15:02
-
-
Save LuviKunG/d554b25fcc5aa30c6dfc83232228bfa2 to your computer and use it in GitHub Desktop.
This is Secrets of Grindea App Instance Main Class. Using for solve the endless math dungeon puzzle in the game.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace LuviKunG | |
{ | |
/// <summary> | |
/// This is Secrets of Grindea App Instance Main Class. | |
/// Using for solve the endless math dungeon puzzle in the game. | |
/// See also: https://www.facebook.com/watch/?v=2592364457709295 | |
/// | |
/// Copyright by Thanut Panichyotai (@LuviKunG) | |
/// https://github.com/LuviKunG | |
/// Free of use. Copy or clone isn't allowed. | |
/// </summary> | |
public class SecretOfGrindeaMathPuzzleSolver | |
{ | |
public enum SignType : byte | |
{ | |
None, | |
Plus, | |
Minus, | |
Multiply, | |
Division | |
} | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Welcome to Secret of Grindea Math Puzzle Solver!"); | |
Console.WriteLine("This app are written by \'Thanut Panichyotai (@LuviKunG)\'"); | |
Console.WriteLine("https://github.com/LuviKunG"); | |
var single = FindArgument(ref args, "single"); | |
while (true) | |
{ | |
var digits = new List<int>(); | |
var signs = new List<SignType>(); | |
Console.WriteLine("Please enter the digits. (please enter twice to perform next action)"); | |
while (true) | |
{ | |
if (ReadLineInt(out int digit)) | |
{ | |
digits.Add(digit); | |
continue; | |
} | |
else break; | |
} | |
if (digits.Count < 2) | |
{ | |
Console.WriteLine("Cannot perform next action because digits is less than two."); | |
Console.WriteLine("Restarting..."); | |
continue; | |
} | |
Console.WriteLine("Please enter the signs. (+, -, *, /)"); | |
do | |
{ | |
if (ReadLineSign(out SignType sign)) | |
signs.Add(sign); | |
else | |
Console.WriteLine("Please enter the following signs +, -, * or / only."); | |
} | |
while (signs.Count < digits.Count - 1); | |
Console.WriteLine("Please enter equalation value."); | |
int equal = 0; | |
if (!ReadLineInt(out equal)) | |
{ | |
Console.WriteLine("Cannot perform next action because digits is less than two."); | |
Console.WriteLine("Restarting..."); | |
continue; | |
} | |
var solution = new List<string>(); | |
foreach (var permutatedDigit in digits.Permutate(digits.Count)) | |
{ | |
foreach (var permutatedSign in signs.Permutate(signs.Count)) | |
{ | |
var value = Calculate(permutatedDigit.ToArray(), permutatedSign.ToArray()); | |
if (equal == value) | |
{ | |
var solutionStr = WriteSolution(permutatedDigit, permutatedSign, value); | |
solution.Add(solutionStr); | |
} | |
} | |
} | |
if (solution.Count > 0) | |
{ | |
if (single) | |
{ | |
Console.WriteLine(solution[0]); | |
if (solution.Count > 1) | |
Console.WriteLine($"And {solution.Count - 1} more solution..."); | |
} | |
else | |
{ | |
for (int i = 0; i < solution.Count; i++) | |
Console.WriteLine(solution[i]); | |
} | |
} | |
} | |
} | |
private static bool FindArgument(ref string[] args, string key) | |
{ | |
for (int i = 0; i < args.Length; i++) | |
if (args[i] == key) | |
return true; | |
return false; | |
} | |
private static bool ReadLineInt(out int result) | |
{ | |
var digit = Console.ReadLine(); | |
if (int.TryParse(digit, out result)) | |
return true; | |
else | |
return false; | |
} | |
private static bool ReadLineSign(out SignType result) | |
{ | |
var sign = Console.ReadLine(); | |
switch (sign) | |
{ | |
case "+": | |
result = SignType.Plus; | |
return true; | |
case "-": | |
result = SignType.Minus; | |
return true; | |
case "*": | |
result = SignType.Multiply; | |
return true; | |
case "/": | |
result = SignType.Division; | |
return true; | |
default: | |
result = SignType.None; | |
return false; | |
} | |
} | |
private static int Calculate(IList<int> digits, IList<SignType> signs) | |
{ | |
if (digits.Count - 1 != signs.Count) | |
throw new AppCalculateException("Cannot perform calculation because the digits and signs value isn't match."); | |
int value = digits[0]; | |
for (int i = 0; i < signs.Count; i++) | |
{ | |
switch (signs[i]) | |
{ | |
case SignType.Plus: | |
value = value + digits[i + 1]; | |
break; | |
case SignType.Minus: | |
value = value - digits[i + 1]; | |
break; | |
case SignType.Multiply: | |
value = value * digits[i + 1]; | |
break; | |
case SignType.Division: | |
value = value / digits[i + 1]; | |
break; | |
default: break; | |
} | |
} | |
return value; | |
} | |
private static string WriteSolution(IList<int> digits, IList<SignType> signs, int value) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
var digit = digits.GetEnumerator(); | |
var sign = signs.GetEnumerator(); | |
digit.MoveNext(); | |
sign.MoveNext(); | |
sb.Append(digit.Current.ToString()); | |
digit.MoveNext(); | |
do | |
{ | |
sb.Append(' '); | |
sb.Append(SignToChar(sign.Current)); | |
sb.Append(' '); | |
sb.Append(digit.Current.ToString()); | |
} | |
while (digit.MoveNext() && sign.MoveNext()); | |
sb.Append(' '); | |
sb.Append('='); | |
sb.Append(' '); | |
sb.Append(value.ToString()); | |
return sb.ToString(); | |
} | |
private static char SignToChar(SignType sign) | |
{ | |
switch (sign) | |
{ | |
case SignType.Plus: return '+'; | |
case SignType.Minus: return '-'; | |
case SignType.Multiply: return '*'; | |
case SignType.Division: return '/'; | |
default: return ' '; | |
} | |
} | |
} | |
public static class MathExtension | |
{ | |
public static IEnumerable<IList<T>> Permutate<T>(this IList<T> sequence, int count) | |
{ | |
if (count == 1) | |
yield return sequence; | |
else | |
{ | |
for (int i = 0; i < count; i++) | |
{ | |
foreach (var p in Permutate(sequence, count - 1)) | |
yield return p; | |
var tmp = sequence[count - 1]; | |
sequence.RemoveAt(count - 1); | |
sequence.Insert(0, tmp); | |
} | |
} | |
} | |
} | |
public sealed class AppCalculateException : Exception | |
{ | |
public AppCalculateException(string message) : base(message) { } | |
} | |
} |
This file contains hidden or 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 is Secrets of Grindea App Instance Main Class. | |
Using for solve the endless math dungeon puzzle in the game. | |
See also: https://www.facebook.com/watch/?v=2592364457709295 | |
Copyright by Thanut Panichyotai (@LuviKunG) | |
https://github.com/LuviKunG | |
Free of use. Copy or clone isn't allowed. | |
*/ | |
function Permutator(array) { | |
let results = []; | |
function permute(arr, memo) { | |
var cur, memo = memo || []; | |
for (var i = 0; i < arr.length; i++) { | |
cur = arr.splice(i, 1); | |
if (arr.length === 0) { | |
results.push(memo.concat(cur)); | |
} | |
permute(arr.slice(), memo.concat(cur)); | |
arr.splice(i, 0, cur[0]); | |
} | |
return results; | |
} | |
return permute(array); | |
} | |
function Execute(number, sign, equal) { | |
let numbers = Permutator(number); | |
let signs = Permutator(sign); | |
for (var n = 0; n < numbers.length; ++n) { | |
for (var s = 0; s < signs.length; ++s) { | |
let number = numbers[n].slice(0); | |
let sign = signs[s].slice(0); | |
let log = LogValue(number, sign); | |
let value = FindValue(number, sign); | |
if (value == equal) { | |
console.log(log); | |
return; | |
} | |
} | |
} | |
} | |
function FindValue(numbers, signs) { | |
let value = numbers.shift(); | |
while (signs.length > 0) { | |
var sign = signs.shift(); | |
if (numbers.length > 0) { | |
if (sign == '+') { | |
value = value + numbers.shift(); | |
} else if (sign == '-') { | |
value = value - numbers.shift(); | |
} else if (sign == '*') { | |
value = value * numbers.shift(); | |
} else if (sign == '/') { | |
value = value / numbers.shift(); | |
} | |
} | |
} | |
return value; | |
} | |
function LogValue(numbers, signs) { | |
let number = numbers.slice(0); | |
let sign = signs.slice(0); | |
let str = ''; | |
while (number.length > 0 || sign.length > 0) { | |
if (number.length > 0) | |
str = str + number.shift(); | |
if (sign.length > 0) | |
str = str + sign.shift(); | |
} | |
return str; | |
} | |
var number = [8, 4, 2, 2, 8]; | |
var sign = ['*', '+', '*', '+']; | |
Execute(number, sign, 40); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment