Search code examples
c#functionfor-looppuzzle

C# Puzzle within a puzzle, can't figure it out


This is the whole puzzle

I need help with this puzzle of C#: Its a fill in the blanks game and the answers can be one of the following to the 7 spaces

Blanks look like this _ and are highlighted & there are 7 of them

The next part of the puzzle is What C# function can i call to replace the for loop and insert to satisfy ‘result == ?’ in the code below:

    var answer = "Please help me with this puzzle please"; 

    var words = new[] { "Please", "help", "me", "with", "this", "puzzle", "please" }; 

    var result = ?

    if ( result == answer )
    Console.WriteLine(“Correct!”);

Im guessing for this you will need somekind of linq query which will just fit in, but how ?

I'm personally mind blown since im not a fan of C#


Solution

  • Solved. Basically just try out a few things, some other things are pretty logical (e.g. the while(wordCount < 4) due to the wordCount incrementing). Other things are just modulo arithmetic stuff which make the challenge a little bit harder. (Like x % 1 is always true).

    using System;
    
    class Programm
    {
        static void Main()
        {
            var answer = "Please help me with this puzzle please"; 
            var words = new[] { "Please", "help", "me", "with", "this", "puzzle", "please" };
    
            var result = "";
            var wordCount = 0;
    
            for (var iCount = 12; iCount > 0; iCount--)
            {
                while (wordCount < 4) //less than because word count get's incremented
                {
                    if (iCount % 1 == 0)
                    {
                        result += words[wordCount]; 
                        result += " ";
                        wordCount++;
                    }
    
                    if ((iCount * 6) == 24)
                    {
                        result += words[wordCount]; 
                        result += " ";
                        wordCount++;
                    }
                    iCount--;
                }
    
                if (iCount % 3 != 1)
                    continue;
    
                result += words[wordCount]; 
    
                if (wordCount != 6)
                    result += " ";
    
                wordCount += 1;
            }
    
            Console.WriteLine("Result: " + result);
    
            if ( result == answer )
                Console.WriteLine("Correct!");
            else
                Console.WriteLine("FAIL!");
        }
    }