-1

I am trying to learn Expression Trees in C#. Can someone help me solve this puzzle found in Math Logic Puzzles by Kurt Smith ISBN 0-8069-3864-1 in C# code using Expression Trees link to First Printing Edition? Or, could it be, that this puzzle is best suited for a different approach? Am I trying to put a slipper on an Elephants foot? enter image description here

Here is the code I have so far

using System.Collections.Generic;
using System.Linq.Expressions;

namespace ExpTreesBasics1
{
class Program
{

    List<Person> ListOfPersons = new List<Person>(); 

    static void Main(string[] args)
    {
        Person _paul = new Person()
        {
            Mountain = new Mountain(),
            Pack = new Pack() { WeightExpression = Expression.Constant(30) }
        };

        Person _daleDorsey = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};

        Person _jimMcGee = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};

         Person _geraldBrown = new Person()
         {
             Mountain = new Mountain() {Height = 124},
             Pack = new Pack() { WeightExpression = Expression.Subtract(Expression.Constant(_daleDorsey.Pack.Weight), Expression.Constant(_jimMcGee.Pack.Weight)) }
         };

         Person _andyStiller = new Person()
         {
             Mountain = new Mountain()
             {
                 HeightExpression = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height))
             },
             Pack = new Pack() {  WeightExpression  = Expression.Divide(.5, Expression.Lambda(Person))}
         };



        var result = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height));

    }
}

public class Person
{
    public Mountain Mountain { get; set; }
    public Pack Pack { get; set; }
}

public class Mountain
{
    public string Name { get; set; }
    public int Height { get; set; }
    public Expression HeightExpression { get; set; }
}

public class Pack
{
    public int Weight { get; set; }
    public Expression WeightExpression { get; set; }
}

}

CodePoesia
  • 45
  • 6
  • Is this a constraint satisfaction problem? Do I need Microsoft Solver Foundation to solve this? https://msdn.microsoft.com/en-us/magazine/dn759446.aspx – CodePoesia Oct 27 '15 at 20:58
  • I've been told this is a constraint problem/programming similiar to http://stackoverflow.com/questions/318888/solving-who-owns-the-zebra-programmatically – CodePoesia Oct 27 '15 at 21:03
  • Expressions aren't going to help you here unless you actually have something to evaluate those expressions and solve the set of equations they represent. I think that yes, you're better off with Microsoft Solver Foundation, and if I remember correctly it doesn't use `System.Linq.Expressions` to represent its equations. All that said, I'm not actually sure what you hope to accomplish with this code... it feels like an XY problem. Is there some larger problem that you want to do that seems analogous to this word problem? – 31eee384 Oct 27 '15 at 21:31
  • Expression trees are primarily a tool for people developing ORM APIs. You do not say *why* you wish to learn about expression trees. I would suggest that instead of taking a problem that has nothing to do with expression trees and trying to solve it with an unsuitable tool, that you pick a problem that is well-suited to expression trees. – Eric Lippert Nov 02 '15 at 14:36

1 Answers1

0

Here's a brute force solution in F#. Not sure this is really the best way of doing it - it works here because the set of combinations of first name / last name / weight / mountain is relatively small (about 1.7m permutations) - but at least it works :)

Posted as a gist because it's a reasonably large code snippet.

https://gist.github.com/isaacabraham/7a4c1a9beba5bb9fd9e0

Isaac Abraham
  • 3,422
  • 2
  • 23
  • 26