logo Sign In

The DanielB Prisoners and Lightbulbs Thread

Author
Time
This is to discuss the computer program algorithm puzzle posed by DanielB in the riddles thread.

It's extremely nerdy, so continue at your own risk. Risk of us nerds.
If you're going to take forever, then I'm having a hotdog!
Author
Time
I'll post here onlly if it keeps extremly nerdy.
“Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is tell them they are being attacked and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country.” — Nazi Reich Marshal Hermann Goering
Author
Time
So my question on the table is:

At some point, you're going to have two guys with points, the counter and some other guy. In order to transfer them, the one will have to go directly before the other. On any two days, each has a 1 in 100 chance of being selected. The consecutive chance is 1 in 10,000, even if you are trying every single day (which you can't, because the day holds a variable). So, even to do just that last exchange, I can't see how it is mathematically possible to average 3,000 days?

Daniel, have you found a solution to this, or am I just too pessimistic about the probability? Does the math work out like the "two people in the room with the same birthday" problem? No...it can't. Hmmm...
If you're going to take forever, then I'm having a hotdog!
Author
Time
Quote

Originally posted by: Starboy

Daniel, have you found a solution to this, or am I just too pessimistic about the probability? Does the math work out like the "two people in the room with the same birthday" problem? No...it can't. Hmmm...


It dosen't, because it's like throwing a dice twice and getting sequential numbers.

It's a good probability problem, the "same birthday" one... It's one of those "I can't belive this is the answer" problems.
“Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is tell them they are being attacked and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country.” — Nazi Reich Marshal Hermann Goering
Author
Time
You don't require the counter to go directly after anyone, ever. You require the counter to go sometime after anyone else trying to get rid of tokens, but not directly after. Once the light is switched on by the person with the tokens, it is only switched off once someone adds the tokens to their own count. Everyone else can ignore the light being on and wait for the counter to enter the room again.
Author
Time
This is a very special moment for me. For the first time, I've run the program all the way through start to finish with all three phases programmed in. This was the result:



It completed during Phase 3 - which is when you'll normally expect it to complete.
Author
Time
great, phase 3 is complete . . . and that means . . .?
Author
Time
It means I can caculate the average running time. At the moment the it's about 3600.

That's using these settings:

Phase1: 30, 20, 18, 16, 15.
Phase2: 750, 450, 400, 250.
Phase3: 450. (450,450,450,450).

It could be possible to do better by tweaking Phase3 further, something like:

Phase3: 550, 350, 350, 450.

Phase1:

Get counter.
Get as many people as possible on zero, by creating other members with counts.

Phase 2:

Group the tokens together in powers of 2. Starting at 2 and ending at 16.

Phase 3:

Count all remaining tokens.

For Phase 3 to work you need to allocate time to count all the 16 grouped tokens to the counter, then all the (remaining) 8 ones, 4 ones, 2 ones and then all the single tokens (there shouldn't be any, but it can happen that there is).
Author
Time
You have two variable holders: the lightbulb and the day. If the counter does not need to know when the light switch was turned on, all he has is the lightbulb variable...

So that's why you have the standard count, so he knows how many to add. Interesting...
If you're going to take forever, then I'm having a hotdog!
Author
Time
*Warbler can't figure it out and screems in frustration*
Author
Time
Here are the full rules for each phase:

Phase 1:

For days 1 to n-1 of an n day cycle:
1. If the light is off and you have one token then remove it and leave the light off.
2. If the light is off and you have zero tokens then turn the light on and add to your count DayNumber - n. Turn the light on. If this was the first cycle then you are now the counter.
3. If the light is off and you are the counter then turn it on. Add Daynumber - n to your count.

4. There is an exception to the above, and that is if on day two of the first cycle a prisoner enters the room for the second time, then he should turn on the light but not pick up his token or designate himself the counter.
5. If on day 3 of the first cycle the light is on, then it means exactly one person has been in the room before you. Therefore add one to your count, and designate yourself the counter.

For day n of an n day cycle:
1. If the light is off then add daynum - n to your total. If this was the first cycle then you are now the counter.
2. If the light is on and this is the last cycle in Phase1 and your count is one and you are not the counter, then leave the light on and your count is now zero.
3. If the light is on, failing any or all of the above conditions then turn it off.

Phase 2:

For days 1 to n-1 of an n day cycle:
For non-counters:
1. If the light is off and you have an odd number of tokens, then remove one token and turn the light on.
2. If the light is on and you have an odd number of tokens, then add one token and turn the light off.

For the counter:
1. If the light is on then switch it off and add one token to the count.

The last day of the cycle:
1. If the light is on and you are not the counter and you have an odd number of tokens, then remove one token and leave the light on.
2. If the light is on, then add one token and turn the light off.

Phase 3:

This is the shortest phase in code. But good luck following it! It works in my head and there isn't any errors in it (like dropping counts and changeovers), I'll try to explain how it works as a set of rules later, but for now:
Author
Time
I still belive I can do your algorithm in a recursive way. I'll try that later.
“Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is tell them they are being attacked and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country.” — Nazi Reich Marshal Hermann Goering
Author
Time
Quote

Originally posted by: Starboy
You have two variable holders: the lightbulb and the day. If the counter does not need to know when the light switch was turned on, all he has is the lightbulb variable...

So that's why you have the standard count, so he knows how many to add. Interesting...
Well he has to know in which specified period the switch was turned on. If the light is on at the end of such a period then the person visiting on the last day must take the tokens. He can, however, turn the light switch on, or leave it on on such days if he leaves the required number of tokens in the light switch.

It is complicated in algorithm form, but on a 1-to-1 basis as described above the prisoners need only follow a set of easy rules. The way I've written them above is generic, the rules they would need to follow are very easy.
Author
Time
Clever clever clever. I like it.
If you're going to take forever, then I'm having a hotdog!
Author
Time
I still don't get it. I guess I'm just stupid
Author
Time
Quote

Originally posted by: DanielB
Here are the full rules for each phase:

Phase 1:

For days 1 to n-1 of an n day cycle:
1. If the light is off and you have one token then remove it and leave the light off.
2. If the light is off and you have zero tokens then turn the light on and add to your count DayNumber - n. Turn the light on. If this was the first cycle then you are now the counter.
3. If the light is off and you are the counter then turn it on. Add Daynumber - n to your count.



this, to me makes no sense. Lets say that the 1st cycle is 20 days. Lets also say on day 10 someone goes into the room for the 2nd time and the light is off. According to your directions, that person is supposed to add to their count(which I guess starts out at zero, you don't indicate where the count starts at), the daynumber(which is 10)-n(which is 20).
0+(10-20)= -10????????????
Author
Time
Quote

Originally posted by: Warbler
Quote

Originally posted by: DanielB
Here are the full rules for each phase:

Phase 1:

For days 1 to n-1 of an n day cycle:
1. If the light is off and you have one token then remove it and leave the light off.
2. If the light is off and you have zero tokens then turn the light on and add to your count DayNumber - n. Turn the light on. If this was the first cycle then you are now the counter.
3. If the light is off and you are the counter then turn it on. Add Daynumber - n to your count.



this, to me makes no sense. Lets say that the 1st cycle is 20 days. Lets also say on day 10 someone goes into the room for the 2nd time and the light is off. According to your directions, that person is supposed to add to their count(which I guess starts out at zero, you don't indicate where the count starts at), the daynumber(which is 10)-n(which is 20).
0+(10-20)= -10????????????


Starts out at one.

Yeah I think you got a point, isn't it N - daynumber?
“Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is tell them they are being attacked and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country.” — Nazi Reich Marshal Hermann Goering
Author
Time
actually I think its

count+(Daynumber-(daynumber of 1st day of current cycle))

if the count starts at 0

(using prevous example) 0 + (10-1)=9

Author
Time
Hmm,

Phase 1:

For days 1 to n-1 of an n day cycle, and where m = number of the 1st day of the n day cycle:
1. If the light is off and you have one token then remove it and leave the light off.
2. If the light is off and you have zero tokens then turn the light on and add to your count DayNumber - m. Turn the light on. If this was the first cycle then you are now the counter.
3. If the light is off and you are the counter then turn it on. Add Daynumber - m to your count.
Author
Time
I've got the average down to ~3510 days...
Author
Time
I think the only way forward from here is to program the program to program it, who votes I do that?
Author
Time
No please don't...
“Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is tell them they are being attacked and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country.” — Nazi Reich Marshal Hermann Goering