0
$\begingroup$

In a village, there are 400 streets, 200 with yellow painted homes and 200 with blue painted homes. Each street has 16 homes as in the blueprint below.

enter image description here

But there are certain sets of rules that should be followed for accommodation in each home.

The Basic Rules for ideal conditions are:

  1. Only one person can be accommodated in a home until all 16 homes on a street are filled by 16 people (one in each); only then can a home be shared by another person.

  2. For a single street, a home is allocated to a person in increasing order, which means that first person arriving on a street would be accommodated in the first home, the second person in the second home, and so on.

  3. Above accommodation rules are independent for each street, which means that streets can be allocated to any person randomly, but he/she would be accommodated in the home as per the above rules. (For example, if a person has been allocated to street 5, and if the first two homes on street 5 are already accommodated then he/she would be accommodated to home #3 of street #5, regardless of whether streets 1 to 4 are empty.)

Now the question is why are streets segregated in two different colors? The reason is that both have a unique set of accommodation rules based on Gender and Age.

  1. Yellow Streets: They reserve homes based on two parameters. First, there is a max of 2 homes reserved for senior citizen (SC) per street. Second, after SC reservation, the remaining homes could be allocated to either male or female. Let's name these two parameters as SC and Mix.

  2. Blue streets: They reserve homes based on three parameters. First, there is a max of 2 homes reserved for SC. This is the same as for yellow streets; the difference is in the remaining homes. Second, a few homes are reserved for females. Third, the remaining homes are reserved for males. Let's say name these three parameters as SC, F and M.

Now who will decide the values of these parameters?

The owner of this village has randomly set the parameters for each street as explained below, and homes should be allocated based on these parameters.

  1. The ideal values of these parameters are: For yellow, SC=2 and Mix=14, i.e., 2 homes (#15 and #16) are reserved for senior citizens and yhr remaining 14 homes (#1 to #14) are for either males or females. For blue, SC=2, F=7 and M=7, i.e., 2 homes (#15 and #16) are reserved for SC, 7 homes (#8 to #14) are reserved for females and the remaining 7 (#1 to #7) are reserved for males.

  2. However, the value of these parameters for each street is different and the summation of these parameters would never be more than 16 and less than 1. For example, for a yellow street, the parameters could be "SC=0, Mix=16" or "SC=1, Mix=15" or "SC=2, Mix=0" or anything else. For a blue street, the parameters could be "SC=1, F=1 and M=3", "SC=0, F=1 and M=0", or any other combination.

  3. Suppose for a yellow street the combination is "SC=1 and Mix=3", then a male or female can be accommodated in any home from #1 to #3 and SC can be accommodated in home #16 only. And the remaining homes (#4 to #15) are locked and no one can use those homes. For blue homes, the rules are applied in same manner, just Mix is further categorized in M and F.

  4. Max possible value for each parameter: In case of blue and yellow streets, the max possible value for SC is 2. And for all the other categories, the maximum possible value is 16.

  5. Special case of SC, if SC=2, then the reserved homes for SC are #15 and #16. If SC=1, then the reserved home is #16 and if SC=0, then there isn't any home reserved for SC on that particular street.

  6. In case of Mix, the reservation of homes would always start from #1 and till the assigned value of Mix. For example, if Mix=15, then #1 to #15 are reserved for Mix.

  7. In case of M, the reservation of homes would start from #1 and till the assigned value of M.

  8. In case of F, the reservation of homes would start from the home number after the last home number of M. For example, if F=13 and M=3, then homes #4 to #16 are reserved for females. and if M=0 and F=2, then homes #1 and #2 are reserved for Females.

  9. Repetition Rule: this is the same rule, as mentioned in basic rules, but more detailed. For a given street with set parameters, the allocation of homes can not be repeated until the reserved home for the same category (or parameter) is filled once. Let's say for Blue Street #2, the parameters are set as SC=1, M=2 and F=3. If the first person to arrive is SC, then he/she would be accommodated at home #16. If the second is Male, then he would be accommodated at # 1, if the 3rd person is again SC, then he/she has to share home #16 with first person. If the 4th is Female, she would be accommodated at #3, and so on.

Now you are appointed as the supervisor of this village and as a supervisor, you have to make sure that none of the above rules are broken, and for that you have one logical-magical stick. If someone breaks a rule, then you can kick him/her out and also charge them 500 bucks. But if you fail to prevent anyone from breaking these rules then owner of this village will fire you and will charge you 10,000,000,000,000 bucks. ;-)

What logic would you apply to that logical-magical stick, so that you can make sure that the accommodation is always aligned with the rules?

Hint:

Supervisor has record of all the arriving persons, their age, gender, street to be allocated including blue/yellow homes and value of parameters (SC, Mix, M, F) for each streets.

$\endgroup$

closed as unclear what you're asking by 2012rcampion, JMP, Glorfindel, Rubio, Techidiot Mar 31 '17 at 16:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ This sounds more like a (rather strange) computer programming exercise than a puzzle. An exercise in interpreting complicated customer requirements, or something. $\endgroup$ – Gareth McCaughan Jan 15 '17 at 22:12
  • $\begingroup$ @Gareth, this isn't exactly a computer programming exercise, but yes I got an idea of this puzzle while facing this kind of problem, though my issue is resolved, but I found it quite interesting so modified the problem, added bit complexity and finally the puzzle is here. And believe me, it's very interesting to solve it and you will definitely scratch your head at one point. 😉 $\endgroup$ – H.Modh Jan 16 '17 at 2:36
  • $\begingroup$ Are senior citizens not considered to be either "male" or "female" (they can't be accommodated in a one-gender home)? $\endgroup$ – Nautilus Jan 16 '17 at 8:52
  • $\begingroup$ It doesn't matter, whether senior citizens are male or female. They can be accommodated in only their reserved homes. Per street max 2 homes are reserved for them. $\endgroup$ – H.Modh Jan 16 '17 at 9:02
  • $\begingroup$ So what is the task here? I'm having problem seeing what is it that we should do. Do we need to assign the parameters? Or are the parameters fixed but unknown to us? I assume we need to set the parameters such that for any sequence of people coming to the village, there is always a way to put that person in without breaking the rules. Is this correct? $\endgroup$ – justhalf Jan 18 '17 at 4:13
1
$\begingroup$

Since this is set of rules for some program, it is hard to write it in a form which is easily readable, but I think that the result is something like this:

SC, F, M, Mix means predefined rule for how many homes are allocated for corresponding person types.
SC counter, F counter, M counter, Mix counter are each street counters for corresponding person types.
If I understand correctly, then the system is always correct so there is no possibility that it sends for example person F to street, where F = 0.

 - If person is SC:
         1. add 1 to SC counter in the street
         2. if SC counter > SC or SC = 1
            2a. then 
                2aI.  if SC = 2 the person can be in the home #15 or #16, else
                2aII. the person can be only in home #16
            2b. else the person can be only in home #(14 + SC counter)
 
       - If person is F:
         1. add 1 to F counter in the street
         2a. if F counter > F then the person can be in any home from #(M + 1) to #(M + F)
         2b. else the person can be only in home #(M + F counter)
 
       - If person is M:
         1. add 1 to M counter in the street
         2a. if M counter > M then the person can be in any home from #1 to #(M) (including M)
         2b. else the person can be only in home #(M counter)
 
       - If person is Mix:
         1. add 1 to Mix counter in the street
         2a. if Mix counter > Mix then the person can be in any home from #1 to #(Mix) (including Mix)
         2b. else the person can be only in home #(Mix counter)

Here is also the original source code in C# which was in original answer:

//If returns true, person is in correct home, if false, person is in wrong home.
private bool IsInCorrectHome(Street street, PersonType person, int home)
{
    var homes = new List();
 
    switch (person)
    {
        case PersonType.SC: homes = street.GetNextSC(); break;
        case PersonType.M:
        case PersonType.Mix: homes = street.GetNextM(); break;
        case PersonType.F: homes = street.GetNextF(); break;
    }
 
    return homes.Contains(home);
}
 
private enum PersonType
{
    SC,
    Mix,
    F,
    M
}
 
private class Street
{
    public int Number { get; set; }
    public int SC { get; set; }
    public int F { get; set; }
    public int M { get; set; } //For M and Mix
 
    private int _arrivedSC;
    private int _arrivedF;
    private int _arrivedM;
 
    public List GetNextSC()
    {
        _arrivedSC++;
 
        if (_arrivedSC > SC || SC == 1)
        {
            return SC == 2 ? new List { 15, 16 } : new List{ 16 };
        }
        else
        {
            return new List { 14 + _arrivedSC };
        }
    }
 
    public List GetNextF()
    {
        _arrivedF++;
 
        if (_arrivedF > F)
        {
            var result = new List(F);
            for (var i = M; i < M + F; i++)
            {
                result.Add(i + 1);
            }
            return result;
        }
        else
        {
            return new List { M + _arrivedF };
        }
    }
 
    public List GetNextM()
    {
        _arrivedM++;
 
        if (_arrivedM > M)
        {
            var result = new List(M);
            for (var i = 1; i <= M; i++)
            {
                result.Add(i);
            }
            return result;
        }
        else
        {
            return new List { _arrivedM };
        }
    }
}

$\endgroup$
  • $\begingroup$ Can you please explain? Some people on PSE (like rand al'thor) can't code. Also, use the <pre> and </pre> tags to do code formatting in spoiler blocks. $\endgroup$ – boboquack Mar 30 '17 at 20:10
  • $\begingroup$ @boboquack thank you for the pre tags. I understand that someone can't code, but it is not easy for me to write program logic in plain text :-). $\endgroup$ – Artholl Mar 31 '17 at 7:38
  • $\begingroup$ Have you ever tried rubber-ducking? It's a good way of checking your code for consistency and to eliminate bugs! $\endgroup$ – boboquack Mar 31 '17 at 7:44
  • $\begingroup$ No, I never heard about that. It looks like a good exercise for me :-). $\endgroup$ – Artholl Mar 31 '17 at 8:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.