Logic puzzle
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Anthony Wang 2024-01-05 20:13:37 -06:00
parent 0a54a08e19
commit 6a1bd601bd
Signed by: a
SSH key fingerprint: SHA256:B5ADfMCqd2M7d/jtXDoihAV/yfXOAbWWri9+GdCN4hQ

View file

@ -0,0 +1,54 @@
---
title: "A Logic Puzzle"
date: 2024-01-05T17:50:00-06:00
description: "A coordination-with-limited-communication logic puzzle"
type: "post"
tags: ["math", "computer-science", "puzzle", "parallel-programming"]
---
This is exercise 1.4 of *The Art of Multiprocessor Programming*:
> You are one of P recently arrested prisoners. The warden, a deranged computer scientist, makes the following announcement:
>
> You may meet together today and plan a strategy, but after today you will be in isolated cells and have no communication with one another.
>
> I have set up a "switch room" which contains a light switch, which is either *on* or *off*. The switch is not connected to anything.
>
> Every now and the, I will select one prisoner at random to enter the "switch room." The prisoner may throw the switch (from *on* to *off*, or vice-versa), or may leave the switch unchanged. Nobody else will ever enter this room.
>
> Each prisoner will visit the switch room arbitrarily often. More precisely, for any N, eventually each of you will visit the switch room at least N times.
>
> At any time, any of you may declare: "we have all visited the switch room at least once." If the claim is correct, I will set you free. If the claim is incorrect, I will feed you all to the crocodiles. Choose wisely.
>
> Devise a winning strategy when you know that the initial state of the switch is *off*.
>
> Devise a winning strategy when you do not know whether the initial state of the switch is *on* or *off*.
First of all, why are they so many puzzles about deranged wardens torturing the brains of prisoners? That aside, let's solve this puzzle!
First, let's try something easier. The simplest possible case is where P is 2 and we know the initial state is off. Take a moment to solve this.
{{< details "Click to show solution" >}}
There are many solutions, but the simplest is for both prisoners to turn the switch on. If the switch is already on and you weren't the one who did it, then the other prisoner must have did it. Thus, they've already visited the switch room, so you can safely declare the everyone has visited the switch room.
{{< /details >}}
Cool, that wasn't too bad. What if P is 2 but we don't know the initial state of the switch? If the switch is already on when you go in, maybe the other prisoner didn't turn it on and the switch just started out on.
{{< details "Click to show solution" >}}
We can't just have both prisoners always turn the switch on, since the switch could start out on. Instead, we can have prisoner 1 always turn it on, and prisoner 2 always turn it off. Here's a hypothetical scenario. Let's say the switch is off initially. If prisoner 1 goes in, they will turn it on. Then, if prisoner 2 goes in, they don't know if the switch started out on or if prisoner 1 did it, so they can't just declare victory now. Anyways, prisoner 2 will turn the switch off. Now if prisoner 1 goes in and sees the switch is off, prisoner 1 can safely declare. Basically, the strategy is to declare once you've modified the switch twice, since the other prisoner must have modified the switch in between your two modifications.
{{< /details >}}
Great, let's take off the training wheels and allow P to be anything, but we'll still require the initial state to be off. Our first strategy obviously doesn't work, but perhaps we could adapt our second strategy?
{{< details "Click to show solution" >}}
It would be nice if we could have prisoner 1 maintain a counter of how many other prisoners have gone in. A prisoner can signal that they've gone in by turning the switch off. Thus, we can have prisoner 1 always turn the switch on, and everyone else will turn the switch off to signal to prisoner 1 that they've gone in. Each time prisoner 1 turns the switch back on, they know one other prisoner went in to turn it off. This doesn't entirely work, because the same prisoner could turn off the switch each time, so we need to restrict the prisoners 2 through P to only turn off the switch once. Now prisoner 1 can declare once they've turned the switch on P times. Prisoner 1 is guaranteed to declare eventually because the problem guarantees that each prisoner will eventually visit the switch room N times for any N. Thus, player 1 won't ever get stuck waiting for the remaining prisoners to turn off the switch.
{{< /details >}}
And finally, let's tackle the version of the problem where P can be anything, and the initial state is unknown.
{{< details "Click to show solution" >}}
Our previous strategy doesn't work if the switch starts out on. In that case, a prisoner might turn if off and prisoner 1 won't know about it. Then prisoner 1 will get stuck because they will wait for the switch to be turned off P-1 times, but it will only be turned off P-2 times since a prisoner already at the beginning used up their opportunity to turn off the switch. To avoid this, maybe we can have each prisoner turn off the switch two times. Then prisoner 1 will wait for the switch to be turned off 2P-3 times before declaring. If the switch starts out off, then it will be turned off by only 2P-3 times, but that means one prisoner will only turn it off once. Fortunately, this is fine, because we're still guarenteed each prisoner visits the switch room once.
{{< /details >}}
I guess the moral of the story is to try special, simple cases first and work your way up to the actual problem. Also, try not to run into deranged computer science enthusiast wardens.