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: