Monday, October 5, 2015

Logic puzzle

Problem:


http://clintonowner.weebly.com/uploads/2/6/1/6/26168794/scan_1.pdf

Code:

namespace ManagedWorkspace
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class Program
    {
        static void Permute(int[] data, List<int> partialSolution, List<List<int>> solution)
        {
            if (partialSolution.Count == data.Length)
            {
                solution.Add(new List<int>(partialSolution));
            }
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] != -1)
                {
                    partialSolution.Add(data[i]);
                    int was = data[i];
                    data[i] = -1;
                    Permute(data, partialSolution, solution);
                    data[i] = was;
                    partialSolution.RemoveAt(partialSolution.Count - 1);
                }
            }
        }

        static void Main(string[] args)
        {
            var solns = GetPermutations();

            var nameLists = new List<List<int>>(solns);
            var careerLists = new List<List<int>>(solns);
            var nationalityLists = new List<List<int>>(solns);
            foreach (var names in nameLists)
            {
                foreach (var careers in careerLists)
                {
                    foreach (var nationalities in nationalityLists)
                    {
                        // names = 1 => Adyen
                        // names = 2 => Brodie
                        // names = 3 => Liliana
                        // names = 4 => Melany
                        // names = 5 => Perla

                        // career = 1 => Architect
                        // career = 2 => Engineer
                        // career = 3 => Doctor
                        // career = 4 => Lawyer
                        // career = 5 => Politician

                        // nationality = 1 => American
                        // nationality = 2 => Bulgarian
                        // nationality = 3 => Czech
                        // nationality = 4 => Serbian
                        // nationality = 5 => Swedish

                        int adyenId = names.Select((name, i) => Tuple.Create(i, name)).Single(tuple => tuple.Item2 == 1).Item1;
                        int lilianaId = names.Select((name, i) => Tuple.Create(i, name)).Single(tuple => tuple.Item2 == 3).Item1;
                        int perlaId = names.Select((name, i) => Tuple.Create(i, name)).Single(tuple => tuple.Item2 == 5).Item1;
                        
                        int czechId = nationalities.Select((nationality, i) => Tuple.Create(i, nationality)).Single(tuple => tuple.Item2 == 3).Item1;
                        int swedeId = nationalities.Select((nationality, i) => Tuple.Create(i, nationality)).Single(tuple => tuple.Item2 == 5).Item1;
                        int bulgarianId = nationalities.Select((nationality, i) => Tuple.Create(i, nationality)).Single(tuple => tuple.Item2 == 2).Item1;

                        int architectId = careers.Select((career, i) => Tuple.Create(i, career)).Single(tuple => tuple.Item2 == 1).Item1;
                        int engineerId = careers.Select((career, i) => Tuple.Create(i, career)).Single(tuple => tuple.Item2 == 2).Item1;
                        int lawyerId = careers.Select((career, i) => Tuple.Create(i, career)).Single(tuple => tuple.Item2 == 4).Item1;

                        if (names[3] != 2)
                        {
                            // Brodie is October
                            continue;
                        }
                        if (nationalities[3] != 1 && nationalities[3] != 5)
                        {
                            // Brodie can only be American or Swedish
                            continue;
                        }

                        if (careers[4] != 5)
                        {
                            // politican is December
                            continue;
                        }
                        if (names[4] == 4 || names[4] == 1)
                        {
                            // politican cannot be Adyen of Melany
                            continue;
                        }

                        if (nationalities[lilianaId] == 3)
                        {
                            // Liliana is not Czech
                            continue;
                        }

                        if (nationalities[lilianaId] == 5)
                        {
                            // Liliana is not Swedish
                            continue;
                        }

                        if (lilianaId == 2 && careers[swedeId] == 1)
                        {
                            // Liliana is born in July and Swede is architect is okay
                        }
                        else if (swedeId == 2 && careers[lilianaId] == 1)
                        {
                            // Swede is born in July and Liliana is architect is okay too
                        }
                        else
                        {
                            // Otherwise not
                            continue;
                        }

                        if (architectId < lawyerId)
                        {
                            // Lawyer's birthday is earlier than architect.
                            continue;
                        }
                        if (swedeId < architectId)
                        {
                            // architect birthday is earlier than Swede's .
                            continue;
                        }

                        if (names[1] == 1)
                        {
                            // Adyen is not born in June
                            continue;
                        }

                        if (nationalities[adyenId] == 3)
                        {
                            // Adyen is not Czezh
                            continue;
                        }

                        if (careers[adyenId] == 2)
                        {
                            // Adyen is not an engineer
                            continue;
                        }

                        if (nationalities[1] == 3)
                        {
                            // Czech is not born in June
                            continue;
                        }
                        if (careers[1] == 2)
                        {
                            // Engineer is not born in June
                            continue;
                        }
                        if (names[1] == 5)
                        {
                            // Perla is not born in June
                            continue;
                        }

                        if (careers[czechId] == 1)
                        {
                            // Czech is not an engineer
                            continue;
                        }

                        if (names[czechId] == 5)
                        {
                            // Perla is not Czech
                            continue;
                        }

                        if (careers[perlaId] == 1)
                        {
                            // Perla is not an engineer
                            continue;
                        }

                        if (bulgarianId > engineerId)
                        {
                            // Engineer's birthday is later than Burgarian 
                            continue;
                        }

                        Console.WriteLine(NameString(names[0]) + " " + CareerString(careers[0]) + " " + NationalityString(nationalities[0]));
                        Console.WriteLine(NameString(names[1]) + " " + CareerString(careers[1]) + " " + NationalityString(nationalities[1]));
                        Console.WriteLine(NameString(names[2]) + " " + CareerString(careers[2]) + " " + NationalityString(nationalities[2]));
                        Console.WriteLine(NameString(names[3]) + " " + CareerString(careers[3]) + " " + NationalityString(nationalities[3]));
                        Console.WriteLine(NameString(names[4]) + " " + CareerString(careers[4]) + " " + NationalityString(nationalities[4]));
                        Console.WriteLine("--------------------------------");

                    }
                }
            }
        }

        private static string NationalityString(int p)
        {
            switch (p)
            {
                case 1: return "American";
                case 2: return "Bulgarian";
                case 3: return "Czech";
                case 4: return "Serbian";
                case 5: return "Swedish";
            }
            return null;
        }

        private static string CareerString(int p)
        {
            switch (p)
            {
                case 1: return "Architect";
                case 2: return "Engineer";
                case 3: return "Doctor";
                case 4: return "Lawyer";
                case 5: return "Politician";
            }
            return null;
        }

        private static string NameString(int p)
        {
            switch (p)
            {
                case 1: return "Ayden";
                case 2: return "Brodie";
                case 3: return "Liliana";
                case 4: return "Melany";
                case 5: return "Perla";
            }
            return null;
        }

        private static List<List<int>> GetPermutations()
        {
            var solns = new List<List<int>>();
            Permute(Enumerable.Range(1, 5).ToArray(), new List<int>(), solns);
            return solns;
        }
    }
}

No comments:

Post a Comment