{"title": "100 doors", "language": "C sharp|C#", "task": "There are 100 doors in a row that are all initially closed. \n\nYou make 100 passes by the doors. \n\nThe first time through, visit every door and ''toggle'' the door (if the door is closed, open it; if it is open, close it). \n\nThe second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it. \n\nThe third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.\n\n\n;Task:\nAnswer the question: what state are the doors in after the last pass? Which are open, which are closed?\n\n\n'''Alternate:''' \nAs noted in this page's discussion page, the only doors that remain open are those whose numbers are perfect squares.\n\nOpening only those doors is an optimization that may also be expressed; \nhowever, as should be obvious, this defeats the intent of comparing implementations across programming languages.\n\n", "solution": "using System;\nusing System.IO;\nusing System.Collections.Generic;\n\nclass Program\n{\n static void Main()\n {\n Console.Clear();\n Console.WriteLine(\"Input a number of doors to calculate, then press enter\");\n StartCalculator();\n }\n \n static void StartCalculator()\n {\n //The number to calculate is input here\n string input = Console.ReadLine();\n Console.Clear();\n \n try\n {\n //The program attempts to convert the string to an int\n //Exceptions will be caught on this line\n int numberOfDoors = Convert.ToInt32(input);\n \n //Will call method recursively if input number is less than 1\n if (numberOfDoors <= 0)\n {\n Console.WriteLine(\"Please use a number greater than 0\");\n StartCalculator();\n }\n \n //The program then starts the calculation process\n Calculate(numberOfDoors);\n \n //After calculation process is finished, restart method is called\n RestartCalculator();\n }\n catch(FormatException)\n {\n //Code will be executed if the number has a decimal or has an unrecognizable symbol\n Console.WriteLine(\"Unable to read. Please use a real number without a decimal\");\n StartCalculator();\n }\n catch (OverflowException)\n {\n //Code will be executed if number is too long\n Console.WriteLine(\"You number is too long\");\n StartCalculator();\n }\n }\n \n static void Calculate(int numberOfDoors)\n {\n //Increases numberOfDoors by 1 since array starts at 0\n numberOfDoors++;\n \n //Dictionary key represents door number, value represents if the door is open\n //if value == true, the door is open\n Dictionary doors = new Dictionary();\n \n //Creates Dictionary size of numberOfDoors, all initialized at false\n for(int i = 0; i < numberOfDoors; i++)\n {\n doors.Add(i, false);\n }\n \n //Creates interval between doors, starting at 0, while less than numberOfDoors\n for (int doorInterval = 0; doorInterval < numberOfDoors; doorInterval++)\n {\n //Will alter every cubby at doorInterval\n //1 needs to be added since doorInterval will start at 0 and end when equal to numberOfDoors\n for(int i = 0; i < numberOfDoors; i += doorInterval + 1)\n {\n //Changes a false value to true and vice versa\n doors[i] = doors[i] ? false: true;\n }\n }\n \n //Writes each door and whether it is open or closed\n for(int i = 0; i < numberOfDoors; i++)\n {\n //Skips over door 0\n if (i == 0) continue;\n //Writes open if door value is true, writes closed if door value is false\n Console.WriteLine(\"Door \" + (i) + \" is \" + (doors[i] ? \"open\" : \"closed\"));\n }\n }\n \n static void RestartCalculator()\n { \n Console.WriteLine(\"Press any key to restart\");\n Console.ReadKey(true);\n Main();\n }\n}\n"} {"title": "100 prisoners", "language": "C sharp|C# from D", "task": "The Problem:\n* 100 prisoners are individually numbered 1 to 100\n* A room having a cupboard of 100 opaque drawers numbered 1 to 100, that cannot be seen from outside.\n* Cards numbered 1 to 100 are placed randomly, one to a drawer, and the drawers all closed; at the start.\n* Prisoners start outside the room\n:* They can decide some strategy before any enter the room.\n:* Prisoners enter the room one by one, can open a drawer, inspect the card number in the drawer, then close the drawer.\n:* A prisoner can open no more than 50 drawers.\n:* A prisoner tries to find his own number.\n:* A prisoner finding his own number is then held apart from the others.\n* If '''all''' 100 prisoners find their own numbers then they will all be pardoned. If ''any'' don't then ''all'' sentences stand. \n\n\n;The task:\n# Simulate several thousand instances of the game where the prisoners randomly open drawers\n# Simulate several thousand instances of the game where the prisoners use the optimal strategy mentioned in the Wikipedia article, of:\n:* First opening the drawer whose outside number is his prisoner number.\n:* If the card within has his number then he succeeds otherwise he opens the drawer with the same number as that of the revealed card. (until he opens his maximum).\n\n\nShow and compare the computed probabilities of success for the two strategies, here, on this page.\n\n\n;References:\n# The unbelievable solution to the 100 prisoner puzzle standupmaths (Video).\n# [[wp:100 prisoners problem]]\n# 100 Prisoners Escape Puzzle DataGenetics.\n# Random permutation statistics#One hundred prisoners on Wikipedia.\n\n", "solution": "using System;\nusing System.Linq;\n\nnamespace Prisoners {\n class Program {\n static bool PlayOptimal() {\n var secrets = Enumerable.Range(0, 100).OrderBy(a => Guid.NewGuid()).ToList();\n\n for (int p = 0; p < 100; p++) {\n bool success = false;\n\n var choice = p;\n for (int i = 0; i < 50; i++) {\n if (secrets[choice] == p) {\n success = true;\n break;\n }\n choice = secrets[choice];\n }\n\n if (!success) {\n return false;\n }\n }\n\n return true;\n }\n\n static bool PlayRandom() {\n var secrets = Enumerable.Range(0, 100).OrderBy(a => Guid.NewGuid()).ToList();\n\n for (int p = 0; p < 100; p++) {\n var choices = Enumerable.Range(0, 100).OrderBy(a => Guid.NewGuid()).ToList();\n\n bool success = false;\n for (int i = 0; i < 50; i++) {\n if (choices[i] == p) {\n success = true;\n break;\n }\n }\n\n if (!success) {\n return false;\n }\n }\n\n return true;\n }\n\n static double Exec(uint n, Func play) {\n uint success = 0;\n for (uint i = 0; i < n; i++) {\n if (play()) {\n success++;\n }\n }\n return 100.0 * success / n;\n }\n\n static void Main() {\n const uint N = 1_000_000;\n Console.WriteLine(\"# of executions: {0}\", N);\n Console.WriteLine(\"Optimal play success rate: {0:0.00000000000}%\", Exec(N, PlayOptimal));\n Console.WriteLine(\" Random play success rate: {0:0.00000000000}%\", Exec(N, PlayRandom));\n }\n }\n}"} {"title": "15 puzzle game", "language": "C sharp 3+", "task": "Implement the Fifteen Puzzle Game.\n\n\nThe '''15-puzzle''' is also known as: \n:::* '''Fifteen Puzzle'''\n:::* '''Gem Puzzle'''\n:::* '''Boss Puzzle'''\n:::* '''Game of Fifteen'''\n:::* '''Mystic Square'''\n:::* '''14-15 Puzzle'''\n:::* and some others.\n\n\n;Related Tasks:\n:* 15 Puzzle Solver\n:* [[16 Puzzle Game]]\n\n", "solution": "using System;\nusing System.Collections.Generic;\nusing System.Drawing;\nusing System.Windows.Forms;\n\npublic class FifteenPuzzle\n{\n const int GridSize = 4; //Standard 15 puzzle is 4x4\n const int BlockCount = 16;\n\n static readonly Random R = new Random();\n\n private List