Created
August 29, 2016 20:38
-
-
Save Thecentury/6c4a6a297b04b754f3f4a188e0546a79 to your computer and use it in GitHub Desktop.
6652
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace _6652 | |
{ | |
class Program | |
{ | |
private const int Used = -1; | |
static void Main( string[] args ) | |
{ | |
int[] digits = new[] { 6, 6, 5, 2 }; | |
int[][] permutations = { | |
new[] {1, 2, 3}, | |
new[] {1, 3, 2}, | |
new[] {2, 1, 3}, | |
new[] {2, 3, 1}, | |
new[] {3, 1, 2}, | |
new[] {3, 2, 1} | |
}; | |
IOp[] operations = { | |
new Operation((x,y)=> x+y, "+"), | |
new Operation((x,y)=> x-y, "-"), | |
new Operation((x,y)=> x*y, "*"), | |
new Operation((x,y)=> x/y, "/") | |
}; | |
foreach ( int[] permutation in permutations ) | |
{ | |
for ( int op1Index = 0; op1Index < operations.Length; op1Index++ ) | |
{ | |
for ( int op2Index = 0; op2Index < operations.Length; op2Index++ ) | |
{ | |
for ( int op3Index = 0; op3Index < operations.Length; op3Index++ ) | |
{ | |
List<IOp> ops = new List<IOp>(); | |
List<int> copy = digits.ToList(); | |
int index1 = permutation[0]; | |
AddOpWithDigits( copy, index1, ops, operations, op1Index ); | |
int index2 = permutation[1]; | |
AddOpWithDigits( copy, index2, ops, operations, op2Index ); | |
int index3 = permutation[2]; | |
AddOpWithDigits( copy, index3, ops, operations, op3Index ); | |
double result = Execute( ops ); | |
string formula = String.Join(" ", ops) + " = " + result; | |
Console.WriteLine( formula ); | |
if ( Math.Abs( result - 17 ) < 0.1 ) | |
{ | |
Debugger.Break(); | |
} | |
} | |
} | |
} | |
} | |
} | |
private static double Execute( List<IOp> ops ) | |
{ | |
Stack<double> values = new Stack<double>(); | |
foreach ( var op in ops ) | |
{ | |
double currentValue = op.Execute( values ); | |
values.Push( currentValue ); | |
} | |
return values.Peek(); | |
} | |
private static void AddOpWithDigits( List<int> copy, int index1, List<IOp> ops, IOp[] operations, int op1Index ) | |
{ | |
var x1 = GetDigit( copy, index1 - 1 ); | |
int x2 = GetDigit( copy, index1 ); | |
AddOp( x1, x2, ops, operations[op1Index] ); | |
} | |
private static int GetDigit( List<int> copy, int index ) | |
{ | |
int x1 = copy[index]; | |
copy[index] = Used; | |
return x1; | |
} | |
private static void AddOp( int y1, int y2, List<IOp> ops, IOp op2 ) | |
{ | |
if ( y1 == Used && y2 != Used ) | |
{ | |
ops.Add( new DigitOp( y2 ) ); | |
ops.Add( op2 ); | |
} | |
else if ( y1 == Used && y2 == Used ) | |
{ | |
ops.Add( op2 ); | |
} | |
else if ( y2 == Used ) | |
{ | |
ops.Insert( 0, new DigitOp( y1 ) ); | |
ops.Add( op2 ); | |
} | |
else | |
{ | |
ops.Add( new DigitOp( y1 ) ); | |
ops.Add( new DigitOp( y2 ) ); | |
ops.Add( op2 ); | |
} | |
} | |
} | |
interface IOp | |
{ | |
double Execute( Stack<double> stack ); | |
} | |
[DebuggerDisplay( "{_str}" )] | |
public class Operation : IOp | |
{ | |
private readonly Func<double, double, double> _op; | |
private readonly string _str; | |
public Operation( Func<double, double, double> op, string str ) | |
{ | |
_op = op; | |
_str = str; | |
} | |
public double Execute( Stack<double> stack ) | |
{ | |
double x2 = stack.Pop(); | |
double x1 = stack.Pop(); | |
return _op( x1, x2 ); | |
} | |
public override string ToString() | |
{ | |
return _str; | |
} | |
} | |
[DebuggerDisplay( "{_digit}" )] | |
public class DigitOp : IOp | |
{ | |
private readonly int _digit; | |
public DigitOp( int digit ) | |
{ | |
_digit = digit; | |
} | |
public double Execute( Stack<double> stack ) | |
{ | |
return _digit; | |
} | |
public override string ToString() | |
{ | |
return _digit.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment