Brewsterware

September 30, 2020

Sudoku cracker for Dynamics 365 Finance and Operations

Filed under: 365 for Finance and Operations — Joe Brewer @ 12:44 pm

It’s been a long time since I have done any coding just for fun, and I rarely do it with X++, but a sudoku cracker for Finance and Operations seemed like a good challenge.

This cracker will crack even the most toughest of puzzles by using recursion and backtracking for when there are multiple possibilities for values in a cell.

/// <summary>
/// Sudoku puzzle cracker
/// </summary>
class SudokuCracker
{
    // this macro allows us to specify the line and column of the grid array
    #localMacro.GridIndex
        (%1 - 1) * 9 + %2
    #endMacro

    private const int TotalRows = 9;
    private const int TotalColumns = 9;

    private int grid[TotalRows, TotalColumns];
    private int cellsFilled;

    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        SudokuCracker cracker = new SudokuCracker();

        cracker.loadGrid();
        cracker.solve();
        cracker.saveGrid();
    }

    /// <summary>
    /// import a file with the known numbers and populate a 2 dimensional array with them
    /// </summary>
    public void loadGrid()
    {
        System.String stringLine;
        str line;
        int gridLine = 0;
        var fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

        using (var reader = new System.IO.StreamReader(fileUpload.openResult()))
        {
            stringLine = reader.ReadLine();

            while (!System.String::IsNullOrEmpty(stringLine))
            {
                line = strKeep(stringLine, '123456789 ');

                if (line)
                {
                    gridLine++;

                    for (int i = 1; i <= TotalRows; i++)
                    {
                        int gridIndex = #GridIndex(gridLine, i);
                        int value = str2Int(subStr(line, i, 1));

                        grid[gridIndex] = value;

                        if (value)
                        {
                            cellsFilled++;
                        }
                    }
                }

                stringLine = reader.ReadLine();
            }
        }
    }

    /// <summary>
    /// create a pretty grid with the numbers filled in, and send it back to the user
    /// </summary>
    public void saveGrid()
    {
        TextBuffer output;

        output = new TextBuffer();

        for (int line = 1; line <= TotalRows; line++)
        {
            output.appendText(strFmt('%1%2%3|%4%5%6|%7%8%9\r\n', 
                grid[#GridIndex(line, 1)], 
                grid[#GridIndex(line, 2)], 
                grid[#GridIndex(line, 3)],
                grid[#GridIndex(line, 4)],
                grid[#GridIndex(line, 5)],
                grid[#GridIndex(line, 6)],
                grid[#GridIndex(line, 7)],
                grid[#GridIndex(line, 8)],
                grid[#GridIndex(line, 9)]));

            if (line mod 3 == 0 &amp;&amp;
                line != TotalRows)
            {
                output.appendText('---+---+---\r\n');
            }
        }

        File::SendStringAsFileToUser(output.getText(), 'solved.txt');
    }

    /// <summary>
    /// method to determine whether a value is valid at a specific line and column
    /// </summary>
    /// <param name="_line">The line number of the grid</param>
    /// <param name="_column">The column number of the grid</param>
    /// <param name="_value">An integer value to be tested</param>
    /// <returns>true if the value is valid, false if not</returns>
    private boolean isValuePossible(int _line, int _column, int _value)
    {
        // check to see whether the value is possible in the line
        for (int i = 1; i <= TotalRows; i++)
        {
            if (grid[#GridIndex(_line, i)] == _value)
            {
                return false;
            }
        }

        // check to see whether the value is possible in the column
        for (int i = 1; i <= TotalColumns; i++)
        {
            if (grid[#GridIndex(i, _column)] == _value)
            {
                return false;
            }
        }

        // check to see whether the value is possible in the square

        // work out the starting point for the square
        int line = real2int(roundDownDec((_line - 1) / 3, 0)) * 3;
        int column = real2int(roundDownDec((_column - 1) / 3, 0)) * 3;

        for (int i = 1; i <= 3; i++)
        {
            for (int j = 1; j <= 3; j++)
            {
                int gridIndex = #GridIndex(line + i, column + j);

                if (grid[gridIndex] == _value)
                {
                    return false;
                }
            }
        }

        return true;
    }

    /// <summary>
    /// recursive backtracking method which fills in the missing numbers to solve the puzzle
    /// </summary>
    public void solve()
    {
        for (int line = 1; line <= TotalRows; line++)
        {
            for (int column = 1; column <= TotalColumns; column++)
            {
                if (grid[#GridIndex(line, column)] == 0)
                {
                    for (int value = 1; value <= 9; value++)
                    {
                        if (this.isValuePossible(line, column, value))
                        {
                            cellsFilled++;
                            grid[#GridIndex(line, column)] = value;

                            this.solve();

                            // have we finished?
                            if (cellsFilled == (TotalRows * TotalColumns))
                            {
                                return;
                            }

                            // start backtracking
                            cellsFilled--;
                            grid[#GridIndex(line, column)] = 0;
                        }
                    }

                    // nothing to see here, move along please.....
                    return;
                }
            }
        }
    }
}

The format of the file that this class uses is an ASCII based grid using pipes, hyphens and plus symbols to separate the nine squares of the sudoku grid. Below is an example that can be used to test the code – copy it into notepad and save it somewhere where the 365FO client can access it.

  9| 85|   
 6 |   |  9
 78|   | 14
---+---+---
   |   |   
  5| 18|   
   |7  |482
---+---+---
   |  7| 4 
2  |6 9|   
 8 |   | 7 

Run the class and choose and upload the file that you created in the above step. After a few moments you will be prompted to download a file which should contain the solution.

Happy cracking!

November 12, 2019

Creating and testing a strongly typed Stack class in X++

Filed under: 365 for Finance and Operations — Joe Brewer @ 12:47 pm

For my latest project in X++ I needed to use a Stack. .NET does already provide a Stack class however MS recommends using the Generics version as it is faster because it is typed. See Non-generic collections shouldn’t be used on GitHub. I had initially tried creating a Stack class using a container, but it was painfully slow – it took several minutes to push and pop 100,000 elements onto and off of the stack.

Here is my implementation:

class Stack extends List
{
    private Types dataType;

    public void push(anytype _value)
    {
        // double check that we have received a variable of the correct type
        if (typeOf(_value) != dataType)
        {
            throw error("Incorrect data type");
        }

        this.addStart(_value);
    }

    public void new(Types _type)
    {
        dataType = _type;

        super(_type);
    }

    public anytype pop()
    {
        ListIterator iterator;
        anytype topValue;

        // double check that there is an element on the stack
        if (!this.elements())
        {
            return null;
        }

        iterator = new ListIterator(this);

        // position the iterator to the top
        iterator.more();

        // retreive the value
        topValue = iterator.value();

        // remove the element from the stack
        iterator.delete();

        // return the value
        return topValue;
    }
}

Here is a job to demonstrate how to use the class and to test the speed of pushing and popping 100,000 elements on to and off of the stack:

class StackTest
{        
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {    
        System.Random random;
        System.Diagnostics.Stopwatch stopwatch;
        System.TimeSpan timeSpan;
        Stack numberStack;
        str elapsedTime;

        random = new System.Random();
        numberStack = new GWStack(Types::Integer);
        stopwatch = new System.Diagnostics.Stopwatch();

        stopwatch.Start();

        // push 100,000 random numbers onto the stack
        for (int i = 0 ; i <= 100000 ; i++)
        {
            numberStack.push(random.Next(1, 1000));
        }

        // pop everything off of the stack one by one
        for (int i = 0 ; i <= 100000 ; i++)
        {
            numberStack.pop();
        }

        stopwatch.Stop();
        timeSpan = stopwatch.Elapsed;

        elapsedTime = System.String::Format("{0:00}:{1:00}:{2:00}.{3:0000}",
            timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds,
            timeSpan.Milliseconds);

        info(strFmt("elapsed time for list based stack: %1", elapsedTime));
    }

}

The results show that pushing and popping 100,000 elements is well under 1 second.

Let me know if you use it or if you found this helpful.

Powered by WordPress