Dyota's blog

C#: 100 prisoners

Translating texts

This is a (almost one-to-one) translation of the "100 Prisoners" solution that I wrote in PowerShell.

Things I learned:

namespace Prisoners
{
    internal class Box
    {
        public int boxNumber;
        public int prisonerNumber;

        public Box(int boxNumber, int prisonerNumber)
        {
            this.boxNumber = boxNumber;
            this.prisonerNumber = prisonerNumber;
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;

namespace Prisoners
{
    internal class Cabinet
    {
        public List<Box> boxes;

        public Cabinet()
        {
            this.boxes = new List<Box>();

            List<int> prisonerNumbers = new List<int>(Enumerable.Range(1, 100).ToArray());

            for(int boxNumber = 1; boxNumber <= 100; boxNumber++)
            {
                int random = new Random().Next(prisonerNumbers.Count);
                this.boxes.Add(new Box(boxNumber, prisonerNumbers[random]));
                prisonerNumbers.RemoveAt(random);
            }

        }
    }
}
using System.Linq;

namespace Prisoners
{
    internal class Prisoner
    {
        public int prisonerNumber;

        public Prisoner(int prisonerNumber)
        {
            this.prisonerNumber = prisonerNumber;
        }
        
        public int checkBoxContents(Box box)
        {
            return box.prisonerNumber;
        }

        public bool searchThroughBoxes(Cabinet cabinet)
        {
            bool found = false;
            int count = 0;
            int startingNumber = this.prisonerNumber;
            int nextNumber = 0;
            string output;

            while (!found && count < 50)
            {
                if (count == 0)
                {
                    nextNumber = checkBoxContents(cabinet.boxes.Where(box => box.boxNumber == startingNumber).ToArray()[0]);
                }
                else
                {
                    nextNumber = checkBoxContents(cabinet.boxes.Where(box => box.boxNumber == nextNumber).ToArray()[0]);
                }

                count++;

                if (nextNumber == this.prisonerNumber)
                    found = true; output = $"Prisoner #{this.prisonerNumber} found in {count} attempts!";
            }

            if (!found)
                output = $"Prisoner #{this.prisonerNumber} search failed, everybody dies.";

            return found;
        }
    }
}
using System;
using System.Collections.Generic;

namespace Prisoners
{
    internal class GroupOfPrisoners
    {
        List<Prisoner> prisoners;

        public GroupOfPrisoners()
        {
            this.prisoners = new List<Prisoner>();
            for (int i = 1; i <= 100; i++)
            {
                Prisoner newPrisoner = new Prisoner(i);
                this.prisoners.Add(newPrisoner);
            }
        }

        public bool everyoneSearchThroughBoxes(Cabinet cabinet)
        {
            bool alive = true;
            bool found = false;

            foreach (Prisoner prisoner in this.prisoners)
            {
                found = prisoner.searchThroughBoxes(cabinet);
                if (!found)
                {
                    alive = false;
                    Console.WriteLine("Search failed, everybody dies.");
                    break;
                }
            }

            return found;
        }
    }
}
using System;

namespace Prisoners
{
    internal class Simulator
    {
        int successes;
        public void Simulate(int lifetimes)
        {
            this.successes = 0;
            for(int i = 0; i < lifetimes; i++)
            {
                Cabinet cabinet = new Cabinet();
                GroupOfPrisoners group = new GroupOfPrisoners();
                bool result = group.everyoneSearchThroughBoxes(cabinet);
                if (result)
                {
                    Console.WriteLine("Everybody lives and goes free!");
                    this.successes++;
                    Console.WriteLine(this.successes);
                }
            }

        }
    }
}
namespace Prisoners
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Simulator simulator = new Simulator();
            simulator.Simulate(1000);
        }
    }
}

#csharp #montecarlo