Home > Back-end >  C Two-Dimensional Array Puzzle
C Two-Dimensional Array Puzzle

Time:09-18

This code has been developed to solve a wooden puzzle problem that I found at a thrift shop, which has numerous wooden wheels inscribed with numbers. These wheels have for the purposes of this program been translated to a two-dimensional matrix, with some additional rules describing the bounds of their relative movement.

The calculational part of the code is working. The configuration layouts that I am receiving from the console are valid within the physical and mathematical bounds of the puzzle. However, there should only be 16^4 possible configurations, and after over 75,000 iterations, each of which being unique (insofar as I can look at what literally approaches a million lines of output), there is still no valid solution.

How could I be missing one?

Bottom line, the rows need to stay ordered in the same sequence, but can be shifted ("rotated") by any number of spaces, though 16 shifts of one space each covers the entire rotary layout of each wheel.

A wheel consists of two concentric radial sets of numbers, some having spaces (null values, but indicated in this code as an "N", and the out set of numbers on a wheel above overlaps the inner set of numbers on a wheel below, such that rows one and two are one wheel (the one that remains mathematically stationary because all other wheels can move relative to it), rows three and four are one wheel (dial is probably a more apt term), hence the turnDial() function is called for two rows of numbers at the same time-ish, except for the final row, which is an independent dial with only one set of values, half of them null (indicative of an open space on the physical wheel, wherin the number on the wheel below it would be the value on the "data_Output" array, representative of the actual physical layout of the dials when stacked according to factory configuration. This sorting out as to whether a value comes from a wheel above or a wheel below is done in checkSum(), which also generates a sum for each column to compare to the required solution (that the numbers of each column must add up to "50" for the puzzle to be solved), and outputs a sum of those sums which on any valid solution would equal 800 (50 * 16) columns, and makes for an easy narrowing down of whether a solution is valid (if there weren't 100,000 of them to sort through).

Anyway, it runs. It outputs data, I just don't see it ever outputting a valid solution, so I'm probably missing something. Wits end. Help would be appreciated.

Console Screen Grab

Pictures of the Puzzle

#include "pch.h"
#include <iostream>


using namespace System;
using namespace std;
using namespace System::IO;

const int depth = 9; // (ROWS)
const int length = 16; // (COLUMNS)

int target_Column_Total = 50;
int The_Grail[length] = { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 };
int Sum[16];

int sum_Total;
long int Iteration;

int data_Input[depth][length] =
{   {16, 4, 7, 0, 16, 8, 4, 15, 7, 10, 1, 10, 4, 5, 3, 15},
    {2, 9, 27, 13, 11, 13, 10, 18, 10, 10, 10, 10, 15, 7, 19, 18},
    { 6, 'N', 10, 'N', 8, 'N', 10, 'N', 9, 'N', 8, 'N', 8, 'N', 9, 'N'},
    { 5, 1, 24, 8, 10, 20, 7, 20, 12, 1, 10, 12, 22, 0, 5, 8 },
    { 0, 'N', 11, 'N', 8, 'N', 8, 'N', 8, 'N', 10, 'N', 11, 'N', 10, 'N' },
    { 20, 8, 19, 10, 15, 20, 12, 20, 13, 13, 0, 22, 19, 10, 0, 5 },
    { 14, 'N', 11, 'N', 8, 'N', 12, 'N', 11, 'N', 3, 'N', 8, 'N', 10, 'N' },
    { 8, 17, 4, 20, 4, 14, 4, 5, 1, 14, 10, 17, 10, 5, 6, 18 },
    { 8, 'N', 16, 'N', 19, 'N', 8, 'N', 17, 'N', 6, 'N', 6, 'N', 8, 'N' } };

int data_Output[5][length];




void checkSum();
void turnDial(int x);




void main()
{
    while (true) {
        for (int i = 0; i < length; i  ) {
            turnDial(2);
            turnDial(3);
            checkSum();
            for (int j = 0; j < length; j  ) {
                turnDial(4);
                turnDial(5);
                checkSum();
                for (int k = 0; k < length; k  ) {
                    turnDial(6);
                    turnDial(7);
                    checkSum();
                    for (int l = 0; l < length; l  ) {
                        turnDial(8);
                        checkSum();
                    }
                }
            }
        }
    }
}
    

 


void checkSum() {

    Iteration  ;

    for (int i = 0; i < 5; i  ) {
        for (int j = 0; j < length; j  ) {
            if (i == 0) {
                data_Output[i][j] = data_Input[i][j];
            }

            if (i == 1) {
                if (data_Input[2][j] != 'N') {
                    data_Output[i][j] = data_Input[2][j];
                }
                else {
                    data_Output[i][j] = data_Input[1][j];
                }
            }


            if (i == 2) {
                if (data_Input[4][j] != 'N') {
                    data_Output[i][j] = data_Input[4][j];
                }
                if (data_Input[4][j] == 'N') {
                    data_Output[i][j] = data_Input[3][j];
                }
            }
            if (i == 3) {
                if (data_Input[6][j] != 'N') {
                    data_Output[i][j] = data_Input[6][j];
                }
                if (data_Input[6][j] == 'N') {
                    data_Output[i][j] = data_Input[5][j];
                }
            }
            if (i == 4) {
                if (data_Input[8][j] != 'N') {
                    data_Output[i][j] = data_Input[8][j];
                }
                if (data_Input[8][j] == 'N') {
                    data_Output[i][j] = data_Input[7][j];
                }
            }
        }

    }

    cout << "Data:" << "\n\n";

    for (int x = 0; x < depth; x  ) {
        for (int y = 0; y < length; y  ) {
            cout << data_Input[x][y] << "\t";
        }
        cout << endl;

    }
    cout << endl;

    cout << "Orienation:" << "\n\n";

    for (int x = 0; x < 5; x  ) {
        for (int y = 0; y < length; y  ) {
            cout << data_Output[x][y] << "\t";
        }
        cout << endl;
    }

    cout << "\n";
    for (int y = 0; y < 5; y  ) {
        for (int x = 0; x < length; x  ) {
            if (y == 0) {
                Sum[x] = 0;
            }
            Sum[x]  = data_Output[y][x];
            if (y == (4)) {


                cout << Sum[x] << "\t";
            }

        }

    }

    sum_Total = { 0 };

    for (int i = 0; i < length; i  ) {
        sum_Total  = Sum[i];
    }

    cout << " Sum: " << sum_Total;
    cout << "\t#: " << Iteration << "\n\n";


    if (Sum != The_Grail) {
        cout << "NO DICE." << "\n\n";
  
    }

    if (Sum == The_Grail) {
        cout << "SUCCESS!" << "\n\n";
        exit(888);
     }



    // int Sum[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };



}

void turnDial(int x) {

    int holder = data_Input[x][(length - 1)];

    for (int i = (length - 1); i > 0; i--) {
        data_Input[x][i] = data_Input[x][i - 1];
    }

    data_Input[x][0] = holder;

}

CodePudding user response:

I haven't dug through everything in detail, but the final comparisons (Sum == The_Grail) will always fail as this is comparing two array pointers not (as one might reasonably expect) each value in the arrays. I would start by looping through the arrays, comparing each element.

Also, are you sure the input data is guaranteed to result in a solution? In other words, is the input data known to be good data or is it just random?

CodePudding user response:

Finally!

Pstephan, thanks for your help, nailed it on iteration 58344.

  • Related