The 24 Game tests one's mental arithmetic.



Task
Write a program that randomly chooses and displays four digits, each from 1 ──► 9 (inclusive) with repetitions allowed.

The program should prompt for the player to enter an arithmetic expression using just those, and all of those four digits, used exactly once each. The program should check then evaluate the expression.

The goal is for the player to enter an expression that (numerically) evaluates to 24.

 Only the following operators/functions are allowed: multiplication, division, addition, subtraction
 Division should use floating point or rational arithmetic, etc, to preserve remainders.
 Brackets are allowed, if using an infix expression evaluator.
 Forming multiple digit numbers from the supplied digits is disallowed. (So an answer of 12+12 when given 1, 2, 2, and 1 is wrong).
 The order of the digits when given does not have to be preserved.


Notes
 The type of expression evaluator used is not mandated. An RPN evaluator is equally acceptable for example.
 The task is not for the program to generate the expression, or test whether an expression is even possible.


Related tasks
 24 game/Solve


Reference
 The 24 Game on h2g2.


use Collection.Generic;
use System.Matrix;
 
class RPNParser {
  @stk : Stack<IntHolder>;
  @digits : List<IntHolder>;
 
  function : Main(args : String[]) ~ Nil {
    digits := List->New()<IntHolder>;
    "Make 24 with the digits: "->Print();
    for(i := 0; i < 4; i += 1;) {
      n : Int := Int->Random(1, 9);
        " {$n}"->Print();
        digits->AddBack(n);
    };
    '\n'->Print();
 
    parser := RPNParser->New();
    if(parser->Parse(System.IO.Console->ReadString(), digits)) {
      result := parser->GetResult();
      if(result = 24) {
        "Good job!"->PrintLine();
      }
      else {
        "{$result}, Try again."->PrintLine();
      };
    }
    else {
      "Invalid sequence"->PrintLine();
    };
  }
 
  New() {
    @stk := Stack->New()<IntHolder>;
    @digits := List->New()<IntHolder>;
  }
 
  method : Op(f : \Func->Calc) ~ Nil {
    if(@stk->Size() < 2) { "Improperly written expression"->ErrorLine(); Runtime->Exit(1); };
    b := @stk->Pop();
    a := @stk->Pop();
    @stk->Push(f(a, b));
  }
 
  method : Parse(c : Char) ~ Nil {
    if(c >= '0' & c <= '9') {
      value : Int := c - '0';
      @stk->Push(value);
      @digits->AddBack(value);
    }
    else if(c = '+') {
      Op(\Func->Calc : (a, b) => a + b);
    }
    else if(c = '-') {
      Op(\Func->Calc : (a, b) => a - b);
    }
    else if(c = '*') {
      Op(\Func->Calc : (a, b) => a * b);
    }
    else if(c = '/') {
      Op(\Func->Calc : (a, b) => { if(b <> 0) { return a / b;  } else { return 0; }; });
    };
  }
 
  method : GetResult() ~ Int {
    if(@stk->Size() = 1) {
      return @stk->Top();
    };
 
    return 0;
  }
 
  method : Parse(s : String, digits : List<IntHolder>) ~ Bool {
    each(i : s) {
      Parse(s->Get(i));
    };
 
    @digits->Rewind();    
    while(@digits->More()) {      
      left := @digits->Get()->Get();
      digits->Rewind();
      found := false;
      while(digits->More() & found = false) {
        right := digits->Get()->Get();
        if(left = right) { 
          digits->Remove(); found := true;  
        }
        else { 
          digits->Next(); 
        };
      };
      @digits->Next();
    };
 
    return digits->IsEmpty();
  }
}
 
alias Func {
  Calc : (IntHolder, IntHolder) ~ IntHolder
}