Skip to content

Instantly share code, notes, and snippets.

@pouyakary
Created September 17, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pouyakary/0bb39386f687222ca9bd to your computer and use it in GitHub Desktop.
Save pouyakary/0bb39386f687222ca9bd to your computer and use it in GitHub Desktop.
//
// Program.cs
//
// Created by Pouya Kary on 2015/9/18
// Copyright (c) 2015 Pouya Kary. All rights reserved.
//
// This code is published under the GNU General Public
// License v3 published by Free Software Foundation.
//
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Kary;
using NCalc;
namespace FunnyThing {
class MainClass {
// ────────────────────────────────────────────────────────────────────────────────────────────────────
/// <summary>
/// The entry point of the program, where the program control starts and ends.
/// </summary>
/// <param name="args">The command-line arguments.</param>
public static void Main ( string[] args ) {
// ────────────────────────────────────────────────────────────────────────────────
Terminal.PrintLn( "The Solver..." );
//
// ─── GETTING THE INPUT ──────────────────────────────────────────────────────────
//
string InputString = Terminal.Input( "Please enter a number with 7 digits: " ).Trim( );
if ( !Regex.IsMatch( InputString , "([0-9]){7}" ) ) {
//
// Someone so much wanted this checking...
//
Terminal.Red( );
Terminal.PrintLn( "The input was not in the currect format" );
Terminal.Reset( );
Environment.Exit( 1 );
}
Terminal.PrintLn( "--> Input in right format." );
//
// ─── FIRST STUFF... ─────────────────────────────────────────────────────────────
//
Terminal.Print( "--> Inintilizing the evnironment: " );
Expression InitTimeExpr = new Expression( "34 + 34 / 3" );
InitTimeExpr.Evaluate();
Dictionary<string, bool> History = new Dictionary<string, bool> { };
Random RND = new Random();
Terminal.PrintLn(" [DONE]");
//
// ─── BODY ───────────────────────────────────────────────────────────────────────
//
Terminal.PrintLn( "--> Process started" );
int TryNumber = 1;
for ( ; ; ) {
ForStart:
//
// Generating a formula and seeing if it exsists...
//
string Formula = FormulaGenerator( InputString , ref RND );
if ( History.ContainsKey( Formula ) ) {
goto ForStart;
} else {
History.Add( Formula , true );
}
//
// Evaluating
//
Expression FormulaExpression = new Expression( Formula );
object EvaluatedFormula = 0;
try {
EvaluatedFormula = FormulaExpression.Evaluate( );
Terminal.PrintLn( "--> Try: " + TryNumber + " -> " + Formula + " = " + EvaluatedFormula + "\n" );
} catch {
Terminal.PrintLn("--> NCalc Error");
}
//
// Checking
//
try {
if ( EvaluatedFormula.ToString() == "100" ) {
Terminal.Print( "--> " );
Terminal.Green( );
Terminal.PrintLn( "FOUND THE ANSWER!" );
Terminal.Reset( );
Terminal.NewLine();
Terminal.PrintLn( " " + Formula);
Terminal.NewLine();
Terminal.NewLine();
Environment.Exit( 0 );
}
} catch {
Terminal.PrintLn( "--> Error catched..." );
}
TryNumber++;
}
// ────────────────────────────────────────────────────────────────────────────────
}
// ────────────────────────────────────────────────────────────────────────────────────────────────────
public static char[] OperatorList = { '+' , '-' , '*' , '/' };
// ────────────────────────────────────────────────────────────────────────────────────────────────────
public static string FormulaGenerator ( string Number , ref Random RND ) {
// ────────────────────────────────────────────────────────────────────────────────
bool DidUseParentheses = false;
bool DidFinishParentheses = false;
StringBuilder Result = new StringBuilder( );
//
// ─── THE FIRST PART ─────────────────────────────────────────────────────────────
//
if ( DoOrDoNotParentheses( ref RND ) ) {
Result.Append( '(' );
DidUseParentheses = true;
}
//
// ─── THE MIDDLE ─────────────────────────────────────────────────────────────────
//
for ( int Index = 0 ; Index < 6 ; Index++ ) {
//
// Adding the Number
//
Result.Append( Number[ Index ] );
//
// Deciding to how to add the operator:
// The operator goes befor open Parentheses
// and after closing match. So we have to
// go with that.
//
if ( DidUseParentheses ) {
// ────────────────────────────────────────────────────────────
if ( DidUseParentheses && !DidFinishParentheses ) {
if ( DoOrDoNotParentheses( ref RND ) ) {
Result.Append( ')' );
DidFinishParentheses = true;
}
}
Result.Append( ChooseOperator( ref RND ) );
// ────────────────────────────────────────────────────────────
} else /* of DidUseParentheses */ {
// ────────────────────────────────────────────────────────────
Result.Append( ChooseOperator( ref RND ) );
if ( !DidUseParentheses ) {
if ( DoOrDoNotParentheses( ref RND ) ) {
Result.Append( '(' );
DidUseParentheses = true;
}
}
// ────────────────────────────────────────────────────────────
} /* end of DidUseParentheses */
} /* for ( int Index = 0 ; Index < 6 ; Index++ ) { */
Result.Append( Number[ 6 ] );
//
// ─── THE LAST ───────────────────────────────────────────────────────────────────
//
if ( DidUseParentheses && !DidFinishParentheses ) {
Result.Append( ')' );
}
// ────────────────────────────────────────────────────────────────────────────────
return Result.ToString( );
// ────────────────────────────────────────────────────────────────────────────────
}
// ────────────────────────────────────────────────────────────────────────────────────────────────────
public static char ChooseOperator ( ref Random RND ) {
return OperatorList[ RND.Next( OperatorList.Length ) ];
}
// ────────────────────────────────────────────────────────────────────────────────────────────────────
public static bool DoOrDoNotParentheses ( ref Random RND ) {
return RND.Next( 5 ) < 2;
}
// ────────────────────────────────────────────────────────────────────────────────────────────────────
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment